How to Add Category Filters to Your Homepage

How to Exclude a Post Category from Your WordPress Homepage

If you use post categories to organize content — for example, an “Events” category — you may not want those posts appearing on the main blog index or homepage. WordPress provides a reliable way to modify the main query before posts are retrieved, allowing you to filter out specific categories without altering individual posts or templates.

Why use the pre_get_posts hook?

The pre_get_posts hook fires just before WordPress runs the main query. By hooking into it you can alter query parameters so the list of posts returned excludes whatever you don’t want shown. This approach is preferred over manipulating template files or using client-side tricks because it changes the database query itself, keeping results consistent across feeds, pagination, and search engine crawls.

Where to add the code

Place the code in your theme’s functions.php file or, better yet, in a small custom plugin so the behavior persists even if you switch themes. Always back up your site or test changes on a staging environment before updating production files.

Simple example: exclude a category by ID

The following example excludes a category using its numeric ID. Replace 123 with the ID of the category you want to hide from the homepage. You can find a category’s ID in the WordPress admin by hovering over the category name in the Categories screen and checking the URL shown in the browser status bar.

is_main_query() && $query->is_home() ) {
        // Exclude category ID 123 (use negative value to exclude)
        $query->set( 'cat', '-123' );
    }
}
add_action( 'pre_get_posts', 'mytheme_exclude_category_from_home' );
?>

Exclude multiple categories

To exclude more than one category, provide a comma-separated list of negative IDs. For example, to exclude categories with IDs 123 and 456, set 'cat' => '-123,-456'. This keeps the logic simple and efficient.

Alternative: use category slug or taxonomy queries

If you prefer to exclude by category slug rather than ID, or if you need more complex conditions, you can use tax_query or the category__not_in parameter. Below is an example using category__not_in with an array of IDs:

is_main_query() && $query->is_home() ) {
        $query->set( 'category__not_in', array( 123, 456 ) );
    }
}
add_action( 'pre_get_posts', 'mytheme_exclude_categories_home' );
?>

For more advanced needs, a tax_query lets you build complex logical conditions for custom taxonomies or combined category rules.

Best practices and considerations

  • Always check is_main_query() to avoid affecting secondary queries (widgets, custom loops, etc.).
  • Include ! is_admin() so the admin screens are not affected by the front-end filtering.
  • Test pagination and related feeds to ensure the exclusion behaves as expected across pages and archive navigation.
  • Consider creating a small plugin for functionality that should remain regardless of the active theme.

Summary

Using the pre_get_posts hook is an efficient and clean method to exclude specific categories from your homepage. It keeps the filtering server-side, helps maintain consistent behavior across pagination and feeds, and is straightforward to implement with a few lines of code. Modify the provided examples to match the IDs or slugs for the categories you want to hide, and test on a staging site before deploying to production.