Exclude Category Posts from Homepage
Learn how to exclude one or multiple categories posts from displaying homepage or blog page of WordPress theme.We tested this code in WordPress twenty fifteen theme and other WordPress themes,it's working perfectly.
Add the following code to your WordPress theme's functions.php file and replace category ID number with your own category ID's.It's very easy to exclude category posts from Homepage in WordPress.
To exclude one category posts from homepage :
//Exclude Category Posts from Home Page
function themeprefix_exclude_category($query) {
if ( $query->is_home() ) {
$query->set('cat', '-11');//add your category number
}
return $query;
}
add_filter('pre_get_posts', 'themeprefix_exclude_category');
Replace -11 with your category ID in the above code.
To exclude multiple categories posts from WordPress home page :
//Exclude Category Posts from Home Page
function themeprefix_exclude_category($query) {
if ( $query->is_home() ) {
$query->set('cat', '-11, -12, -25');//add your category number
}
return $query;
}
add_filter('pre_get_posts', 'themeprefix_exclude_category');
Replace -11, -12 and -25 category ID's with your own categories ID's.
If you find any difficulties to add this code,let me know.I will help you.