You can use a custom query in WordPress to retrieve any data that is in the database. Using get_posts and WP_Query are 2 easy custom queries you can write to pull data out of the WordPress database. I show the code I used to generate the recent posts on this website – without a plugin!
WordPress recent posts using custom queries
The two methods I found best to display the 3 most recent posts were to use get_posts and WP_Query.
Here is a custom query using WP_Query. You can use this custom query inside or outside of the loop. The first code block calls WP_Query and 3 posts in the code category while excluding sticky posts. It also selects ‘posts’ post types as opposed to pages or custom post types, but you could choose those as well.
You can query on any database field that you need. For me, I wanted to display the 3 most recent published posts in my Code Snippets category.
Lines 10-12 uses the new instance of WP_Query ($query) to query the specific posts from the database.
<?php
$query = new WP_Query( [
'post__not_in' => get_option( 'sticky_posts'),
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'code',
'posts_per_page' => 3,
] );
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post(); ?>
The code block below is the HTML for the post details along with some necessary PHP functions. Since WP_Query works both inside and outside of the loop, you’ll need to decide if it’s best used in the loop or outside of it.
Don’t forget to use wp_reset_postdata or it may mess up the page below the query or break the page.
<div class="recent-row">
<?php the_post_thumbnail() ?>
<a class="recent-posts-link" href="<?php the_permalink(); ?>"
rel="bookmark"><h4><?php the_title(); ?></h4></a>
<?php get_template_part( 'template-parts/content', 'author' ); ?>
<?php the_excerpt(); ?>
<div class="recent-post-widget"></div>
<a class="recent-posts-button" href="<?php the_permalink(); ?>">
Read more...</a>
<?php
}
}
wp_reset_postdata();
?>
However, if you are on a page that has the loop, then you might as well take advantage of the post data. The loop is fetching the database fields for $post, so use either WP_Query or a get_posts() query while in the loop.
The get_posts query does not work unless it is inside the loop. Here is the code for get posts that returns the same data as WP_Query.
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'front-page' ); ?>
<aside class="bgcolor4"></aside>
<div class="container"></div>
<h3 class="custom-title">
<?php esc_html_e('Latest Articles ', 'tower') ?>
</h3>
<div class="row"></div>
<?php
$posts = get_posts([
'post_type' => 'post',
'category_name' => 'code',
'posts_per_page' => 3,
]);
foreach ($posts as $post) {
setup_postdata($post); ?>
<div class="recent-row">
Lines 6-9 are the HTML elements and classes to be output for the container of the posts, then the custom get_posts query starts on line 10 and continues to line 18. You should notice the div.recent-row which starts the HTML in the code block above for WP_Query.
Final thoughts
Using a custom query to pull the most recent posts is best done in a plugin. I put mine directly into my template files since it’s my theme and my website, though I will be creating a plugin for the recent posts (look for that article).
There are a number of different parameters you can query other than the ones I used (post_type, category_name, posts_per_page). Go to the WP_Query developer’s page to see other parameters you can use.