StackTips

Show Posts from Specific Categories to Appear on WordPress HomePage

stacktips avtar

Written by

Editorial,  2 min read,  2.53K views, updated on Sept. 17, 2023

Quick WordPress code snippet to show only the posts from specific categories to appear on homepage recent posts. The landing page of most of the news or personal blogging sites usually display the recent posts achieve of the blog unless the front page is set to a static HTML page.

Your blog might have many different categories, but could be annoying to users who just want to show the recent posts from specific categories to appear on homepage recent posts. The following code snippet will help you to do that.

Add the following snippets to your WordPress theme functions.php file:

function filter_home_post_categories($query) {
    if ($query->is_main_query() && is_home()) {
        $query->set('category_name','cat_slug1, cat_slug2, cat_slug3');
    }
}
add_action('pre_get_posts', 'filter_home_post_categories');

Save the changed andfunctions.php file and visit your WordPress site home page. Notice that you will see only the posts from the categories as specified in the above function.