Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
25 Million+
Websites using our plugins
Years of WordPress experience
WordPress tutorials
by experts

How to Display Recent Posts From a Specific Category in WordPress

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

Do you want to display recent posts from a specific category on your WordPress site?

Filtering posts by category allows you to show more relevant recent posts, which can help to reduce bounce rate and increase pageviews.

In this article, we’ll show you different ways to display recent posts from a specific category in WordPress.

How to Display Recent Posts From A Specific Category In WordPress

Why Filter Posts by Category in WordPress

Displaying recent posts allows users to discover new content as they browse through your website. This greatly helps increase your blog traffic and page views.

It can also help your site’s search engine optimization by making your content more discoverable and improving your interlinking between blog posts.

To make your list of recent posts even more useful, you may want to only show posts relevant to the content users are currently viewing.

Showing recent articles from a category to boost pageviews

This simple step can encourage visitors to spend more time on your site, which can even lead to more email subscribers and customers.

Now, if you want to display all your posts from a specific category on a separate page, WordPress already takes care of this for you.

To find the category page, you simply need to go to Posts » Categories » View page and click on the ‘View’ link below a category.

View posts by category

That being said, let’s take a look at how to easily display recent posts by category in different areas of your website. We’ll cover the following methods, so you can jump to the one that interests you:

Method 1: Adding Recent Posts by Category Using Block Editor

By default, WordPress comes with the latest posts block in the block editor. It allows you to easily display recent posts inside a page or post, and you can even filter them by category.

First, you need to edit the post or page where you want to display the recent posts by category. On the post edit screen, click on the add new block button (+) and then look for the ‘latest posts’ block.

Latest posts block

You will see the block appear in the content area with a preview of your recent posts. Simply click to select the block, and you’ll see block settings in the right column.

At the top of the settings, you will see different options like showing featured image, post author, date, and content.

Sort by category

Now, you need to scroll down to the Sorting and filtering section under the block settings. From here, you need to enter the name of the category you want to show posts from.

You can even select multiple categories by separating category names with a comma.

You’ll notice the preview of your recent posts will change to only include posts from the categories you have chosen.

You can now save your post or page and preview it in a browser window. Here is how it looked on our test site showing recent posts from a specific category with featured images and post excerpts.

Displaying latest posts by category

Method 2: Adding Recent Posts by Category in WordPress Sidebar

This method is recommended for users who want to display recent posts from specific categories in their WordPress sidebar.

Simply go to the Appearance » Widgets page and add the ‘Latest Posts’ block to your sidebar.

Adding latest posts block to a sidebar widget

By default, the block will show your most recent posts. You edit the block settings and scroll to the ‘Sorting & Filtering’ section.

From here, you can choose the category that you want to display posts from.

Select category

Optionally, you can choose to show featured image, excerpt, author, and more.

Don’t forget to click on the ‘Update’ button to publish your changes.

You can now visit your website to see your recent posts filtered by category.

Recent posts by category in sidebar

Method 3: Adding Recent Posts by Category Using Code

This method requires you to manually add code to your WordPress website. If you haven’t done this before, then see our guide on how to copy and paste code snippets in WordPress.

First, you need to copy and paste the following code in your WordPress theme’s functions.php file or a code snippets plugin.

function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array( 
    'category_name' => 'travel', 
    'posts_per_page' => 5 
) ); 
// The Loop
if ( $the_query->have_posts() ) {
    $string .= '<ul class="postsbycategory widget_recent_entries">';
    while ( $the_query->have_posts() ) {
            if ( has_post_thumbnail() ) {
            $string .= '<li>';
            $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
            } else { 
            // if no featured image is found
            $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
    } else {
    // no posts found
 $string .= '<li>No Posts Found</li>';
$string .= '</ul>';
return $string;
/* Restore original Post Data */
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');

Don’t forget to replace ‘travel’ with your own category slug (the category name used in URLs) and save your changes.

Tip: You can also add multiple categories separated by a comma.

We recommend adding this code using WPCode, the best code snippets plugin. It allows you to safely add code in WordPress, without editing your functions.php file. So, you don’t need to worry about breaking your site.

To get started, you need to install and activate the free WPCode plugin. If you need help, see this tutorial on how to install a WordPress plugin.

Upon activation, go to Code Snippets » + Add Snippet from the WordPress dashboard.

This will bring you to the Add Snippet page. From here, find the ‘Add Your Custom Code (New Snippet)’ option and click the ‘Use Snippet’ button underneath it.

Add new snippet

Next, add a title for your snippet, which can be anything to help you remember what the code is for.

Then, paste the code from above into the ‘Code Preview’ box and select ‘PHP Snippet’ as the code type from the dropdown list on the right.

Paste snippet into the WPCode plugin and choose code type

After that, toggle the switch from ‘Inactive’ to ‘Active’ and hit the ‘Save Snippet’ button.

Activate and save snippet in WPCode

This code simply asks WordPress to display 5 recent posts from the category ‘news’. It then displays those posts as a bullet list with thumbnail sized featured images.

Lastly, it creates a shortcode [categoryposts] that you add to a page, post, or sidebar widget. Next, we’ll show you how to use this shortcode.

Using Shortcode to Add Recent Posts by Category to Posts and Pages

Simply edit the post or page where you want to display the recent posts by category. On the post edit screen, click on the add new block (+) button and then add the Shortcode block to your content area.

Adding shortcode to display posts from a category

Next, you need to add the shortcode [categoryposts] in the block settings.

You can now save your post or page and preview it to see your recent posts from specific category in action. This is how it looked on our test site.

Posts by category code method with no styling

As you can see, the styling doesn’t look so neat. Don’t worry, you can change that by adding some custom CSS code to your theme.

Simply, go to Appearance » Customize page to launch the theme customizer and then switch to the ‘Additonal CSS’ tab in the left column.

Adding custom CSS code to your theme

You can add the following custom CSS code as a starting point.

ul.postsbycategory {
list-style: none;
.postsbycategory li {
.postsbycategory img {

Don’t forget to click on the ‘Publish’ button to save your custom CSS code. You can now visit your post or page to view your recent posts styled a bit more neatly now.

Recent posts by category with styling

Adding Recent Posts by Category Shortcode to WordPress Sidebar

You can add the same shortcode to your WordPress sidebar or any widget-ready area.

Simply go to the Appearance » Widgets page and add the ‘Shortcode’ block to your sidebar.

Adding shortcode to sidebar

You can now paste your shortcode [categoryposts] to the block settings. Don’t forget to click on the Update button to store your widget settings.

You can now visit your website to see your recent posts by category widget in action. Here is how it looked on our test website.

Sidebar recent posts by category preview

We hope this article helped you learn how to display recent posts from a specific category in WordPress. You may also want to see our guide on how to show personalized content to different users in WordPress, and our list of the best WordPress block plugins to further customize your site.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

33 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

    • WPBeginner Support says

      If you mean the date the post was published, the recent post block has an option where you can enable that to display!


  2. Victoria says

    Thank you for a super helpful post! I always find your content really useful. I’ve added the “Latest Posts” block to a new page and entered the category I’d like to appear on that page. It worked! Question though: how can I display more than 100 posts? Is there any way to do that? The number seems to be capped… thanks again! Any help you can offer is greatly appreciated. Perhaps I need a plug-in for this?

    • WPBeginner Support says

      That many posts on one page could slow your site down which is why there is normally a cap. We do not have a recommended plugin for listing that may posts at one time at the moment.


  3. Jade New says

    Another great post, thanks a lot – I was messing around this for hours yesterday when it’s so super easy! Your blog is in my bookmarks.

  4. Erietta says

    Thanks for this article and showing all the options. I have created two different article landing pages for a website each with different categories using a block. So far so easy. Unfortunately this meant that the default blog archive with all its useful controls didnt suit my needs. What I need now is the ability to add pagination to these pages — but I can’t. Any tips?

    • WPBeginner Support says

      For the moment, you would need to create category pages for the pagination you’re looking for. You can do that by adding a category to your menu.


  5. Rob says

    If i have multiple categories, say categories X,Y & Z, and add them to the snippet code – will this display ONLY the posts for that category – so if I’m viewing a post from category X, it will only display other posts from category X and not from category Y & Z… likewise, if i am in Y it will only display posts from Y?

  6. Akshay says

    The font style is totally different from the default. How to change it as per the theme or what is the css for it.

    • WPBeginner Support says

      You would want to reach out to your theme’s support to ensure there isn’t conflicting styling.


    • WPBeginner Support says

      Hi Ledge,

      These lines are probably added by your theme using CSS. You can use Inspect tool to find out the CSS responsible for that and then add custom CSS to override this.


  7. Allen S says

    I have just started to use Recent Posts Extended widget but for some reason i can’t get it do display what i want and where i want it to go. I run a driving school and have built my website using WP & Divi Builder. I have 7 Recent Posts Extended. One for latest customer reviews to appear on home website page for all areas served, 3 for test passes and 3 for customer reviews for 3 areas I cover which have their own page. But when I go to select the area in “limit to category” for each Recent Posts Extended I expected to see 7 categories with check boxes and there are only 5 categories available for selection. I am no developer, so understanding CSS code to use is like reading Chinese. I have trawled the web and not found the answer to suit my needs.

  8. Alex M. says

    Hello, thanks for this snippet. Is there any way to display posts from a specific category but if there isn’t enough with that category it will show the regular posts?

    For example I have one post with the “featured” category checked but on my homepage I need to display 3 posts, that leaves me with 2 empty spots.

    Thanks! :)

  9. Shahbaz says

    If I want to display post of perticular category on a new page then where should i copy those snippets

  10. Mags Nixon says

    I’ve just activated this plug in and worked fine for the first sidebar. The second customised sidebar just showed the category of the first one. I ended up deleting the second one but now it’s not showing at all. Have you had any other feedback about this widget now not being supported by wordpress since the latest updates? Any advice would be appreciated.

  11. Zelaphas says

    Thanks for this. I’m using this with the “Sydney” WP theme and trying to have certain categories of projects on the home page as portfolio/case studies. I successfully got the widget to show along with my desired posts, but they’re displaying vertically. I’d like them to be right next to each other horizontally in a grid (unless on mobile, of course). Any ideas on this? Thanks!

  12. hailemaryam says

    thank you!!! you saved my life i was working on customers website and the need only news to be showed on the slider. you showed me a way how to customize it.

  13. Debora says

    Hi, thank you for the tutorial. Only a question: I’m trying to get a carousel of latest posts in homepage with preview image. All plugins I have tried had bad image layout, I have many images with different sizes. So I wanted to try without plugin, only write code in wp.
    Can you please tell how to add preview images to your code, to display recent posts with preview image? Thank you very much

  14. ben says


    Did you try this plugin (last update 1 year ago) with the last update of WordPress?
    When I check the support in wordpress, it’s seems this plugin is not support anymore and some users can’t use it correctly with recent WordPress.

    Best Regards

  15. Theo Dorant says

    If you want to have your code in functions.php, you can use

    function include_posts_from_recentPostWidget_by_cat() {
    $include = array( ‘cat’ => ‘2, 5’ );
    return $include;

    2 and 5 are the IDs of the categorys you want to show. You can do the same to exclude categories by using ‘-2, -5’

  16. Blair Jersyer says

    What if that post where we’re showing the recent posts is part of recent post and if we’re show the_content()… There won’t be unlimited loop ?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.