How to exclude a specific category from blog posts in WordPress but if a blog post has two categories selected, one from excluded category and the other from included I would like to show the blog post.
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-7' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
I tried using this function in function.php, it hides the excluded category but when two categories are selected for one blog post, it still hides
Advertisement
Answer
/* you may try this code and use category__not_in function */
function exclude_posts_from_home_page( $query )
{
if( $query->is_home() )
{
$query->set( 'category__not_in', array( 7 ) ); // here array of all category ids
}
}
add_action( 'pre_get_posts', 'exclude_posts_from_home_page' );