When using categories for internal purposes (e.g. filter loop) on a page, there are a few important changes that need to be applied:

1. Exclude the category on the main loop (Blog page)

function exclude_category_from_blog_loop( $query ) {
    if (is_home('id') && $query->is_main_query() ) {
        $query->set( 'cat', '-id1, -id2' );
    }
}
add_action( 'pre_get_posts', 'exclude_category_from_blog_loop' );

* is_home() is the function for the blog homepage and is different than is_front_page()

2. Hide category from Sidebar Categories Widget

function exclude_widget_categories($args){
    $exclude = 'id1, id2';
    $args['exclude'] = $exclude;
    return $args;
}
add_filter('widget_categories_args','exclude_widget_categories');

3. Exclude The Category from creating the Archive page (archive.php)

function exclude_category_from_archive( $query ) {
    if ( $query->is_archive() && $query->is_main_query() && ! is_admin() ) {
        $query->set( 'cat', '-id1' );
    }
}
add_action( 'pre_get_posts', 'exclude_category_from_archive' );

4. Remove the category from Sitemap

Here’s the solution applied for Yoast: Edit – Category – Yoast Metabox – Advanced

Similar Posts