# Leave Noindexed Content Out of Site Search

> Hides posts and pages that you set to noindex in Yoast SEO or Rank Math from your own site search, such as thank-you pages and thin landing pages.

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

## Code

```php
add_filter( 'posts_where', function ( $where, $query ) {
    global $wpdb;

    if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() ) {
        return $where;
    }

    // Yoast SEO: "Allow search engines to show this content" set to No.
    if ( defined( 'WPSEO_VERSION' ) ) {
        $where .= " AND NOT EXISTS ( SELECT 1 FROM {$wpdb->postmeta} wpide_ny WHERE wpide_ny.post_id = {$wpdb->posts}.ID AND wpide_ny.meta_key = '_yoast_wpseo_meta-robots-noindex' AND wpide_ny.meta_value = '1' )";
    }

    // Rank Math: "No Index" ticked in the robots meta.
    if ( defined( 'RANK_MATH_VERSION' ) ) {
        $where .= " AND NOT EXISTS ( SELECT 1 FROM {$wpdb->postmeta} wpide_nr WHERE wpide_nr.post_id = {$wpdb->posts}.ID AND wpide_nr.meta_key = 'rank_math_robots' AND wpide_nr.meta_value LIKE '%noindex%' )";
    }

    return $where;
}, 10, 2 );
```

## Notes

Uses each post's own robots setting; content type-wide noindex settings in the SEO plugin are not checked.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Leave Noindexed Content Out of Site 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/
