How to Exclude a Category from Your WordPress Homepage

Sometimes you may not want to display posts from one or more categories to be listed on your home page. You can do this by adding this tiny piece of code to your functions.php file located in your themes directory.

To exclude categories from your home page paste the following snippet in your functions.php file.

//Exclude a Category from Your Homepage
function exclude_cat_from_home($query) {
	if ( $query->is_home ){
		$query->set('cat', '-2,-25,-78');
	}
	return $query;
}
add_filter('pre_get_posts', 'exclude_cat_from_home');

Replace 2,25 and 78 with your own category Ids and don’t forget to put a minus sign in front of them.

See Also : How to Remove Default WordPress Widgets and Clean Up Your Widgets Page

Leave a Comment.