# Exclude a Category From Search Results

> Keeps posts from the categories you choose, such as internal updates or sponsored posts, out of the site search results.

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

## Code

```php
// Category IDs to leave out of search results.
$wpide_search_excluded_categories = array( 0 );

add_action( 'pre_get_posts', function ( $query ) use ( $wpide_search_excluded_categories ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() ) {
        return;
    }

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

## Notes

Use category IDs. Posts stay visible in their category archives.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Exclude a Category From Search Results" 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/
