By default, the homepage of your WordPress site shows posts from all the categories.
However, it is possible to exclude certain categories from being displayed on your homepage, but there's no option to do that in WordPress out of the box.
I wanted to hide posts from the deals category on my site because a lot of affiliate programs misunderstand internet marketing blogs that post coupons as "deals sites", which they do not allow.
To avoid confusion, I decided to hide all the posts from the deals category from my site's home.
In this tutorial, I will show you how to create a category filter without using a plugin.
You need to edit your site's functions.php file and add the following code:
function hide_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-9' );
}
return $query;
}
add_filter( 'pre_get_posts', 'hide_category_home' );
Replace 9 with your category's ID. Don't remove '-'
Use the below code to hide posts from multiple categories:
function hide_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-9, -69, -23' );
}
return $query;
}
add_filter( 'pre_get_posts', 'hide_category_home' );
Click save and you're done!
Top comments (0)