How to Exclude Specific Categories from the WordPress Blog Page

September 10, 2025
How to Exclude Specific Categories from the WordPress Blog Page

By default, WordPress shows all posts on the main blog page. However, you may want to hide certain categories—for example, private announcements, promotional posts, or test content. You can exclude categories from the blog page with a few simple code snippets or plugins.

Method 1: Using pre_get_posts in functions.php

The most common method is to modify the main query to exclude specific categories by ID.

// Exclude categories from the main blog page
function my_exclude_categories_from_blog( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-5,-9' ); // Exclude categories with IDs 5 and 9
    }
}
add_action( 'pre_get_posts', 'my_exclude_categories_from_blog' );

Notes:

  • Use negative IDs to exclude categories.
  • To get a category ID, go to Posts → Categories in the dashboard, hover the category name, and check the URL (e.g., tag_ID=5).

Method 2: Exclude Categories from the Loop Only

If you want more control and don’t want to affect the global query, modify the loop in index.php or home.php:

<?php
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 10,
    'category__not_in' => array( 5, 9 ) // Exclude categories by ID
);
$custom_query = new WP_Query( $args );

if ( $custom_query->have_posts() ) :
    while ( $custom_query->have_posts() ) : $custom_query->the_post();
        the_title( '<h2><a href="' . get_permalink() . '">', '</a></h2>' );
        the_excerpt();
    endwhile;
    wp_reset_postdata();
else :
    echo '<p>No posts found.</p>';
endif;
?>

Method 3: Hide Categories from RSS Feeds as Well

You may also want to exclude the same categories from your RSS feed:

// Exclude categories from feeds too
function my_exclude_categories_from_feed( $query ) {
    if ( $query->is_feed ) {
        $query->set( 'cat', '-5,-9' );
    }
}
add_action( 'pre_get_posts', 'my_exclude_categories_from_feed' );

Method 4: Use a Plugin (No Code)

If you prefer not to edit theme files, you can use plugins:

Summary

  1. Use pre_get_posts to exclude categories from the main blog page.
  2. Use category__not_in inside custom loops for finer control.
  3. Exclude categories from feeds with a feed-specific filter.
  4. Alternatively, use a plugin like Ultimate Category Excluder for a code-free approach.

By excluding categories, you can control what content appears on your blog page and ensure visitors see only the posts you want to highlight.

Avatar

Written by

satoshi

I’ve been building and customizing WordPress themes for over 10 years. In my free time, you’ll probably find me enjoying a good football match.