How to Exclude Pages from WordPress Search Results


By default, WordPress Search feature displays published posts and published pages in search results. Often when users are looking for something in a blog, it is most likely a post rather than a page. In this article, we will show you how to make your search more relevant and less crowded by excluding pages from WordPress search results.

Exclude pages frm WordPress search results

First, you add the following code to your theme’s functions.php file or a site-specific plugin.

  //Exclude pages from WordPress Search  if (!is_admin()) {  function wpb_search_filter($query) {  if ($query->is_search) {  $query->set('post_type', 'post');  }  return $query;  }  add_filter('pre_get_posts','wpb_search_filter');  }

Explanation: This code first checks that the search is not originating from the WordPress admin pages. If it is not, then it searches for posts by setting the post_type parameter.

You can also make it do the opposite by setting the post_type to pages, so it only return pages in the search results.

We hope this article helped you learn how to exclude pages from WordPress search results. You may also want to see our list of best WordPress search plugins to improve your site search.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.