Exclude WordPress category from blog list

Sometimes you would like certain categories of your WordPress blog not to appear on your blog articles list. Here's how to remove articles of some certain categories from appearing in your blog list.

 

1. Find the category ID

Go to Posts -> Categories , click on your category to open it for editing. No inspect the url, find the category ID:

/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=35&post_type=post

 

2. Edit functions.php of your theme

I assume you already know how to edit your theme's functions.php and not go into teaching that. 

Here's the code for your functions.php

function exclude_category($query) { if ( $query->is_home() ) { $query->set('cat', '-xx'); } return $query; } add_filter('pre_get_posts', 'exclude_category');

And to remove multiple categories from the blog list, just do this with the category setting line:

$query->set(‘cat’, ‘-124 -125 -126’);

 

About me

Iinstructor of robotics (LEGO Mindstorms), computer teacher, IT support, web dev, currently studying cyber-physical systems engineering.

If you found a solution here, would you consider donating $25,000 for my next project?

Just kidding, 1$ would do fine.

Hope you found what you needed. Thanks for stepping by.