Query To Display Latest 10 Posts In WordPress

You might be creating your WordPress projects and wanted to display the latest 10 posts added in your site out of the WordPress default loop, for eg: as the breaking news, right? Well, so you searched for this feature and accidentally landed on this page. It seems that you are totally in luck with this. We are here to describe about how to display latest 10 posts in your WordPress projects.

WordPress query

So, what is really WordPress query? It is simply the WordPress feature to query the WordPress posts as well as the pages, which can be displayed outside of the main loop with different ways as you like for your site. It is mainly useful when in your site you need, for eg: to display the posts in widgets as well as list some of the latest posts via breaking news feature.

Utilize WordPress query to display latest 10 posts added in your site

So, now after knowing about the WordPress query, now we will describe about how to display latest 10 posts from your site using this same feature.

First of all, what you have to do is, you can create a function to display the latest 10 posts in your site. Since, if you create the function for the same, then, you can utilize the same function in different places and in different ways. But, if you do the same in a single file, then, utilizing it to many places of your site seems quite difficult. So, lets move ahead and paste this PHP code in the functions.php file of your theme:

function themeprefix_latest_posts() {
global $post;
$latest_posts = new WP_Query(array(
'posts_per_page' => 10, // Displays the latest 10 posts, change 10 to what you require
'post_type' => 'post', // Pulls posts from 'post' post type only
'ignore_sticky_posts' => true, // Ignores the sticky posts

while ($latest_posts->have_posts()) :

<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

// Reset Post Data

Now, after adding this code only, you can notice that you can’t see anything in the front-end, right?. This is so since, we have created the function for this feature and the function is called nowhere in your developing project files. So, let’s move ahead and open up the required file from your project to display the latest 10 posts from your site and paste the below code in that file to check:

<h2><?php esc_html_e('Latest:', 'theme-textdomain'); ?></h2>

<?php themeprefix_latest_posts(); ?>

Now, after this code is added, since we have called that function inside it, hence the latest 10 posts will get displayed from your site. You can see the output for this feature in your site similar to as image below:


Now, if you have followed the above tutorial properly and is success on displaying the latest 10 posts from your site, then, we can assume that you are more familiar on this feature than before. But, if you are still confused and want more help on this feature to get it right for your site, then, please drop the comment below and we will follow it up.

Post navigation

Bishal Napit

Bishal Napit is a WordPress theme developer from Tansen, Palpa, with a passion to learn more on WordPress.

One thought on “Query To Display Latest 10 Posts In WordPress

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.