# Exclude Specific Pages From Search

> Hides chosen posts or pages (thank-you pages, landing pages, legal pages) from front-end search results.

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

## Code

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

    // IDs of the posts or pages to hide.
    $excluded = array( 0 );

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

## Notes

Replace 0 with the IDs to hide.

## How to use it

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