By default, the WordPress blog shows all posts from all categories. But in some situations, you want to exclude posts from a specific category.
To hide the category from the WordPress blog page we need to modify the WordPress loop. We will modify the loop by using the pre_get_posts hook.
Add the following code snippet in your themes functions.php file. Replace category ids with the category id want to exclude.
function hide_category_blog( $query ) {
if ( $query->is_home ) {
$query->set( ‘cat’, ‘-7, -43’ );
}
return $query;
}
add_filter( ‘pre_get_posts’, ‘hide_category_blog’ );
You can obtain category id by editing category. You will find the category id in the URL.