DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Would you like to be able to control which post or pages must be exclued from searches on your WordPress blog? If yes, this recipe will probably help you a lot. Just read on.

To achieve this recipe, simply paste the following code on the functions.php file from your theme. In this example, posts with IDs 8 and 15 will be excluded from your blog’s search results:

function SearchFilter($query) {
    if ($query->is_search) {
    return $query;


Credits goes to Kyle Eslick for this awesome hack!


  1. Hi,

    Thank you for this snippet! But so far it does not seem to work. I am using WP 3.2.1.

    I want to remove 3 pages from my search results. However, the snippet does not seem to be working for pages. I have added in the IDs of my pages, but they still appear in my search results. I have also tried it for posts, but it does not remove posts from the search results.

    Any ideas? (There is no date on this snippet, so I am not sure how old it is)

  2. @nicholas
    the code in this post is partially wrong, if you want to exclude a cat (eg cat id 5 and 24) you have to set
    if you want to exclude a post set


  3. Is there a way to exclude a specific page by Page ID #?

  4. Yes. To exclude a page change



    $query->set(‘post__not_in’, array(215));

    where 215 is the ID of the page you want to exclude. You can exclude as many pages as you want, just add the IDs to the array. You can also use this to exclude posts.

  5. Hi. Is there a way to reverse this code? Exclude everything and and add the post_id I want to include? I have tons of post I want to exclude from search results so it would be efficient to make everything non-searchable and add only the post_id I want to include.

    Big thanks.

  6. Hi, Thanks for this code but I can’t seem to omit specific pages from showing in the search results. The following is the code I am using. Any help would be appreciated.

    function SearchFilter($query) {
    if ($query->is_search) {
    $query->set(‘post__not_in’, array(1449,1447,1445,1443,1441,1439,1437,1435,1433,1431,438));
    return $query;

    Am I doing something wrong?

  7. Updated.

    // search filter
    function fb_search_filter($query) {
    if ( !$query->is_admin && $query->is_search) {
    $query->set(‘post__not_in’, array(40, 9) ); // id of page or post
    return $query;
    add_filter( ‘pre_get_posts’, ‘fb_search_filter’ );

  8. Hi… I just tried the last bit of code, from Wallace… But it’s not working for me. I am pretty desperate to eliminate some pages and posts from search, as they are landing pages specifically made for Google and de-indexed from the googlebot.

    I think WP should build in: if the page is no-index, don’t include it in search.

  9. Rick Buczynski

    Excellent! `post__not_in` filter worked for me:

    function sth_search_filter($query) {
    return $query;

    In my case there are 4 posts serving as `static blocks` which get loaded onto the home page. Those post IDs are registered with WP in the Dashboard under Appearance->Theme Options. I don’t want these to be searchable, so my first line in the function grabs those IDs and puts them into the $excluded_posts array.

    Thanks for the cool tip!

  10. I’ve noticed that unless caching is turned off, the changes do not reflect immediately. So either turn caching off or open a new browser and attempt your changes.

    Once I did that, the code with the top post and the modified comment worked perfectly.

  11. Excellent code. I used the $query->set(‘cat’,’-5,-24′); and it worked like a charm!

  12. Is there any way I can exclude by custom field

    say my custom field meta_key = url and meta_value = ‘vvv.com’ and I want to exclude all these results?

    Is it possible?


    or something?

    Help appreciated!

  13. For those struggling making this work, there are two issues:

    1 Be sure to clear out cache
    2 The above code works, but it has inaccurate coding

    This will work. I replaced the ‘ within the other codes:

    function fb_search_filter($query) {
    if ($query->is_search) {
    $query->set('post__not_in', array(123,23) ); // id of page or post
    return $query;
    add_filter( 'pre_get_posts', 'fb_search_filter' );

Leave a Comment

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