How to Exclude a Category from Your WordPress Homepage

In one way or another, you want to hide some posts belonging to one category on the homepage of WordPress blog.  This little bit of code help you to do that in a nice way.

Write the below code in the file ( Appearance-> Editor ->Theme Functions – functions.php). You can do the same with FTP by editing the physical file.

So I am going to exclude my categories 4,6,9 from the homepage

function exclude_categories($query) {
if ( $query->is_home ) {
$query->set('cat', '-4,-6,-9');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_categories');

Note: Do not edit the third party theme’s functions.php directly, instead of that use a sub theme to modify your own.

 

 

Editorial Staff
 

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.