Quick WordPress code snippet to show only the the posts from specific categories to appear on home page 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.

WordPress front page settings

-Advertisement-

 

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 home page 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 functions.php and make visit your your WordPress site home page. Notice that you will see only the posts from the categories as specified in the above function.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.

Comments are closed here.