Find out how to exclude posts from a particular category on any WordPress theme blog page using code.
By default, the blog page displays all posts, regardless of which category they belong to.
Interested in having a particular category removed from the main view? No problem.
You can do this easily using WordPress conditional tags.
Snippet to exclude a category from the blog
Add the following snippet to the end of the functions.php file:
To see this and another 1097 code snippets of this website, login or subscribe here.
Thanks to this snippet, entries from the category with ID 10 will not be displayed on the blog page.
To find out the ID, go to:
Posts > Categories > Choose the one you want and click on edit.
If you take a look at the url, you will see something like this:
https://osomcode.com/wp-admin/term.php?taxonomy=category&tag_ID=10&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory
Indeed, in this case the category is ID 10.
If you want to hide more than one category you just have to add them to the top code separated by commas, modifying this line like this (for example):
$query->set( 'cat', '-10, -13, -55' );
Note that if you use a child theme that applies the front-page.php template on the home page, even if you add this change on the blog the home page posts section will still show all the categories.
If you want to exclude a category from the home page (front-page), take a look at this tutorial.
Conclusion
Now, thanks to WordPress conditional tags, you know how to hide a blog page category (or categories) in any theme.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉