HIDE POST CATEGORY FROM BLOG AND SIDEBARS IN WORDPRESS

Use the following snippets of code to remove all posts by category from different sections of your WordPress site.

 

Add this code to your theme’s function file being careful not to remove or edit any other code in there!

Hide posts with a certain category from your blog index page:

				
					function blog_exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-5' ); //add multiple categories by separating with commas '-5,-6,-8' ...
}
}
add_action( 'pre_get_posts', 'blog_exclude_category' );

				
			

Hide category link from the sidebar widget:

				
					function widget_exclude_category($args){
$exclude = "5"; // IDs of excluding categories add multiple categories by separating with commas '5,6,8' ...
$args["exclude"] = $exclude;
return $args;
}

				
			

Hide posts with a certain category from the recent posts widget in the sidebar:

				
					function recentPostWidget_exclude_category() {
$exclude = array( 'cat' => '-5' ); //add multiple categories by separating with commas '-5,-6,-8' ...
return $exclude;
}
add_filter('widget_posts_args','recentPostWidget_exclude_category');