# Exclude a Category From the Blog Page

> Hides posts in chosen categories from the main blog page. The posts remain visible in their own category archive.

- Type: PHP
- Runs: Run everywhere
- Category: [Archives](https://wpide.com/library/category/archive/)
- Tags: archive, category, blog, query
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/exclude-category-from-blog/

## Code

```php
add_action( 'pre_get_posts', function ( $query ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() ) {
        return;
    }

    // IDs of the categories to hide.
    $excluded = array( 1 );

    $query->set( 'category__not_in', array_merge( (array) $query->get( 'category__not_in' ), $excluded ) );
} );
```

## Notes

Replace 1 with your category IDs (1 is Uncategorized on most sites).

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Exclude a Category From the Blog Page" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
