Hide a certain Category’s Posts from the Home/Blog page in WordPress
To hide a certain Category’s posts from the home or blog page you just need to know the category ID and then use a filter on the pre_get_posts hook to exclude those posts.
To find the ID for the Category just go to the Category and hover over the edit button and the ID appears in the URL at the bottom.
add_action( 'pre_get_posts', 'themeprefix_exclude_category' ); // Exclude Category Posts from Home Page function themeprefix_exclude_category( $query ) { if ( $query->is_home() ) { $query->set( 'cat', '-338' );//add your category number } return $query; }
Then add the function inside your functions.php file. The above function only runs on the home page and will remove any posts that belong in the category numbers listed. In the example above that would be number 338, to exclude more than one category just comma separate all the ID numbers like so; ‘-338, -340, -350’
The reverse of this is also true, if you wanted to only show a particular category then use the same code but without the minus…
add_action( 'pre_get_posts', 'only_portfolio_category' ); // Only Portfolio Category function only_portfolio_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '3' ); } }
So in this example, only the posts in a category id of ‘3’ would show.