Exclude Pages from WordPress Search Results

a few seconds read

The native WordPress search feature by default displays pages in the search results. The feature is meant for site-wide searches, but could be annoying to users who just want to display only the posts in the search results.

I have this small code snippet working for me from day one to exclude pages from the searches. It goes in the functions.php file of your WordPress theme:

function search_tweak( $query ) {
  if( $query->is_search ) {
    $query->set( 'post_type', 'post' );
  return $query;
add_filter( 'pre_get_posts', 'search_tweak' );

Save the changed functions.php and make a few searches on your WordPress site. You won’t see any pages in the searches anymore.

If looking for a plugin to do that for you, Simply Exclude may help.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

Although WordPress traditional search doesn’t provide search quality as good as Google Custom Search, however many WordPress users still use it, as it is the default search and you don’t need to customize your theme to add search feature on your blog. Recently...
The default WordPress search is the only thing that used to annoy me a lot and I wasn’t using it on any of my WordPress sites. There are a number of reasons back then to stop using WordPress search feature, and the best one for me was it throwing unrelated lin...
To exclude a page from the sitemap in WordPress, you can manually modify the code in your theme's functions.php file. Here's how you can do it:Access your WordPress dashboard and navigate to "Appearance" -> "Theme Editor."On the righ...