How to List Recent Posts from a Single Category in WordPress

Home » Snippets » How to List Recent Posts from a Single Category in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you interested in showcasing recent posts from a specific category on your WordPress site? This function can not only help improve your site’s navigation but also keep your visitors engaged by highlighting fresh content from a particular section. In this guide, we will take you through an easy-to-follow process on how to list recent posts from a single category. This technique will allow you to gain better control over how your content is presented and distributed on your site. So, let’s get started!

					function wpturbo_recent_posts_by_category($category='') {
    $args = array( 
        'post_type' => 'post',
        'post_status' => 'publish',
        'category_name' => $category,
        'posts_per_page' => 5,
    );

    $recent_posts = new WP_Query( $args );
    if ( $recent_posts->have_posts() ) :

        while ( $recent_posts->have_posts() ) : $recent_posts->the_post();
            get_template_part( 'template-parts/content', 'excerpt' ); 
        endwhile;
        wp_reset_postdata(); 
        
    endif;
}

wpturbo_recent_posts_by_category('category-name');
				

The code snippet begins by defining a function called wpturbo_recent_posts_by_category(). This function takes one parameter, $category, which will be used to signify the WordPress category’s name from which the posts need to be fetched.

function wpturbo_recent_posts_by_category($category='') {...}

Next, inside the function, we set up an associative array $args. This array contains the arguments or parameters needed to form a customized query for fetching posts.

$args = array( 
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => $category,
    'posts_per_page' => 5,
);
  • 'post_type' => 'post' specifies that we want to retrieve ‘post’ type content.
  • 'post_status' => 'publish' filters the posts, ensuring we only retrieve posts that are currently published.
  • 'category_name' => $category specifies that we want posts from the category name we passed as the $category parameter.
  • 'posts_per_page' => 5 sets a limit on the number of posts to fetch, in this case, the 5 most recent posts.

Following the declaration of these arguments, we then create a new WP_Query object and pass in our arguments ($args).

$recent_posts = new WP_Query( $args );

Once we’ve built our query, we test if any posts meet the given conditions by calling the have_posts() method on the $recent_posts object. If the query does return posts, the if statement will pass, and we can start the loop with while( $recent_posts->have_posts() ).

if ( $recent_posts->have_posts() ) :

    while ( $recent_posts->have_posts() ) : $recent_posts->the_post();

In the loop, each time $recent_posts->the_post() is called, it sets up a global post data structure, which holds the current post’s data in the loop. We can then use the get_template_part() function to include our post’s content using the ‘excerpt’ template.

get_template_part( 'template-parts/content', 'excerpt' ); 

This will fetch a file called content-excerpt.php from the template-parts directory of your theme and display the post’s excerpt.

Once all posts have been processed, we must call wp_reset_postdata(). This function cleans up the global post data structure, restoring the original main query’s data. It’s a good practice to use this function after a custom query loop to prevent any accidental errors or issues with other loops or queries on the page.

wp_reset_postdata(); 

After defining this function, we can easily display recent posts from any category, by simply calling wpturbo_recent_posts_by_category('category-name');, replacing 'category-name' with the target category’s slug.

Register an account to save your snippets or go Pro to get more features.