Use the coupon code WORDPRESS and save 30% OFF! Buy Now

How to exclude certain posts or pages from your search results

Posted On
How to exclude certain posts or pages from your search results WordPress template

The WordPress search results, by default, contain information from all available posts, pages, and custom post types. In some cases you might feel that you need to exclude certain items from the search results, because you feel that they contain irrelevant or confusing information. Today we’ll take a look at how we can achieve this.

The code

Removing individual items

We’ll start with a way to remove individual posts, pages etc. Edit your theme’s functions.php file and paste this at the bottom.

add_action( 'pre_get_posts', 'my_search_exclude_filter' );
function my_search_exclude_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 3, 11, 234, 553 ) );
}
}

Modify the array bit in the highlighted line, and replace the numbers with the ids of the posts, pages, or custom post types you want to exclude from the search results page.

The function above will make sure that you are executing a search in the main query outside the administration pages. It will then pass the array of ids you have entered to the post__not_in parameter of the WP_Query so they won’t be retrieved. That’s pretty much it.

If you need help finding the id of a certain item, please take a look at this article.

Removing entire post types

What if you want to exclude entire post types from the search results? No problem, paste in this code instead.

add_action( 'pre_get_posts', 'my_search_exclude_filter' );
function my_search_exclude_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$searchable_post_types = get_post_types( array( 'exclude_from_search' => false ) );
$post_type_to_remove = 'page';
if( is_array( $searchable_post_types ) && in_array( $post_type_to_remove, $searchable_post_types ) ) {
unset( $searchable_post_types[ $post_type_to_remove ] );
$query->set( 'post_type', $searchable_post_types );
}
}
}
Exclude post types from the search results

In short what we do here is, first we get all the searchable post types in line 4, then in line 5 we fill in the slug of the post type we want to exclude from our search results. Finally in line 7 we remove it from the array of searchable post types and we instruct WordPress to search in the post types remaining in the array.

14 responses to “How to exclude certain posts or pages from your search results”

  1. Norbert says:

    Hi Nik,

    Thanks for sharing this. Would like to exclude some certain post types from my search results and would rather use the above code than a plugin.

  2. Hi,

    I want to exclude all posts, all categories and all tags.

    Any code for that, please? That would be much appreciated.

    Thank you,
    Harpinder Singh

    • Nik Vourvachis says:

      Hello.

      What you could do is modify the code to this:

      add_action( ‘pre_get_posts’, ‘my_search_exclude_filter’ );
      function my_search_exclude_filter( $query ) {
      if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
      $query->set( ‘post_type’, array( ‘page’ ) );
      }
      }

      In the above example only results from pages will be returned, you can include other custom post types in the array, however if you don’t add ‘post’ in it, no posts will be returned.

  3. Dee says:

    nice tutorial. can we exclude custom post type by id too ? thanks

    • Nik Vourvachis says:

      Thank you!

      You custom posts, most likely, won’t appear in the search results. You could check this on how to include custom post types in the search results. Once they’re in, you can exclude certain custom posts by ID, just as shown above.

  4. John says:

    That’s great but there is also an excellent plugin that does the same job: Search Exclude, for those who prefer plugins.

    • Gerasimos Tsiamalos says:

      Yeap, that will work as well. If you don’t want to create another dependency though, a custom code snippet is all you need.

  5. Nikos Kavvadas says:

    Thank Niko!
    I used already!

  6. Mog Lie says:

    Hej Niko,
    my question code!

    add_action( ‘pre_get_posts’, ‘my_search_exclude_filter’ );
    function my_search_exclude_filter( $query ) {
    if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
    $db_post_id = get_option(‘db_value_from_textfield_plugin’);
    $query->set( ‘post__not_in’, array( $db_post_id ) ); // is not work
    }
    }

    Do you have an idea about this?

    • Mog Lie says:

      I found my answer – thanks.

      Version array_filter

      PHP-Code:
      $db_option_value = esc_attr( get_option(‘my_posts_id’) );
      $arr_filter = array_filter(explode(“,”, $db_option_value));
      $query->set(‘post__not_in’, $arr_filter );

      Version array_map

      PHP-Code:
      $db_option_value = esc_attr( get_option(‘my_posts_id’) );
      $arr_map = array_map(‘trim’,explode(“,”,$db_option_value));
      $query->set(‘post__not_in’, $arr_map );

  7. AJ Clarke says:

    Hey,

    Good snippet, but it would be good to also make a check for $_GET[‘post_type’] and make sure it’s empty before custom setting the allowed post types to prevent possible conflicts with plugins.

  8. Andy Globe says:

    Thanks. This worked for me. I wanted to exclude gallery appears in search results and this code does exactly what I need. Thanks

  9. Jak zrobić says:

    This is what I was looking for. I wanted to exclude 1 category from the search results. There is no such feature inside wordpress itself. Now I know how to deal with it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get access to all WordPress themes & plugins

24/7 Support Included. Join 115,000+ satisfied customers.

Pricing & Sign Up

30-day money-back guarantee. Not satisfied? Your money back, no questions asked.

Back to top