Exclude page from search results

Is there a way to remove certain pages from search results?

1 Like

Hi There!

You have two options that I could find. One is the below php code snippet. You would replace the id’s with the page (or post) ids that you wish to remove from search.

function remove_pages_from_search($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post__not_in', array(3031,2958,2926));
    }
  }
}

add_action('pre_get_posts','remove_pages_from_search');

Or you could use the following plugin (it is paid).
Search WP

I hope that helps!

Thanks @captivation! I wasn’t able to get the snippet to work for some reason, but I did find another plugin that’s free. This one works perfectly!

1 Like