How to Exclude Pages from WordPress Search Results

How to Exclude Pages from WordPress Search Results featured image

Reading Time: 1 min, 13 sec

WordPress search shows both posts and pages in search results. But pages don’t add much value in search. So, you can exclude pages from search results. In this article, I will tell you how to exclude pages from WordPress searches. I will also tell you the way to exclude only certain pages from the search.

If you want to prevent all pages from displaying in the search result, you can use the following code snipped and add it to the functions.php file.

Best WordPress Hosting

Bluehost is one of the best and secure WordPress hosting provider
function twg_search_filter($query)
{
if ($query->is_search)
{
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','twg_search_filter');

If you also have a few custom post types on your blog, you can set those custom posts in the code.

There’s a good WordPress plugin called Search Exclude. It lets you exclude pages and posts from WordPress searches. If you only want to exclude certain pages, use this plugin. This plugin adds an option to the page or post editor. You can select the checkbox to exclude a page from the search.

Now you know how to exclude all pages or certain pages from WordPress search. If you have a large website where you have several pages like terms, checkout page, profile page, and others that make no sense in search, exclude them from search.

Deepanker profile image

Written by Deepanker

Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.

Leave a Reply

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

Need a Hand with Your WordPress Site?

Don't let technical issues slow you down. Our professional WordPress maintenance service handles updates, security, and performance optimization so you can focus on what you do best: creating content.

Learn More About Our Services