Exclude Certain Category Posts From the Blog Page

The correct way to exclude categories from main blog page, is to make use of pre_get_posts which change the query variable before the main query is executed. The following is an example;

function exclude_category($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set(‘cat’, ‘-3,-6’);
    }
}
add_action(‘pre_get_posts’, ‘exclude_category’);

For the full list of available parameters you can use with pre_get_posts, please visit WP_Query codex page.

Categories: Tutorials

Please Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.