Blog

How To Exclude Pages From WordPress Search Results

How To Exclude Pages From WordPress Search Results

WordPress is an awesome platform in many aspects, but there is one room that needs to be improved for its internal search.

Most of the users are agreed with the fact that WordPress search features do not always deliver the best results.

WordPress search feature displays published pages and published posts in search results. Mostly when users are looking for something in your blog, they are usually looking for your post rather than a page.

There are many posts in search results that you might want to avoid from being shown in the search results.

Or maybe you want to avoid certain posts from being shown in search results. These posts could be your old posts or some kind of irrelevant results.

To get rid of these irrelevant results, you'll need to exclude pages from WordPress searches. In this blog post, I will show you how you can make the search feature more relevant and less crowded on your WordPress website.

How to Exclude Pages from WordPress Search Results?

First, you need to add the following code to your website specific plugin or your theme's functions.php file.

//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');
}

This code first thoroughly check the search is not originating from WordPress admin pages. If it is not then it starts searches for posts by setting the post_type parameter.

You can also make it possible by setting the post_type to pages, so it only returns pages in the WordPress search results.

Follow These Steps to Implement Code:

1st Step: – Login to your WordPress dashboard.

2nd Step: – After login, navigate to the Theme Editor section under the Appearance menu and click on Theme Editor button.
how to exclude pages from wordpress search results

3rd Step: – Now click on the function.php file on the right side below the Theme Files section.

how to exclude pages from wordpress search results

4th Step: – Now paste the above-mentioned code in the bottom in the function.php file and click on the Update File button to save changes.

Final Words

If you want to provide a better user experience and improve the visibility of your website then you should know how to exclude pages from WordPress searches. Because by default, WordPress search feature displays published pages and published posts in search results.

This will give a negative impact on users when they are searching for something on your website. So it is essential for your exclude pages from the WordPress searches.

In this blog post, I have discussed the easiest way to exclude pages from WordPress search results. If you want to pages appearing on your WordPress search results.

Leave a Reply


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

Related Posts


how to hide a wordpress page from google

April 3, 2020

How You Can Hide/Noindex A WordPress Page From Google and Other Search Engines

Facebook Twitter LinkedIn Pinterest Shares As you already know how renowned and powerful WordPress is. But here is the one most amazing thing about WordPress is its hiding feature. WordPress allows you to hide/noindex a particular page and post from Google and other search engines (Bing, Yahoo etc.).

woocommerce vs prestashop

March 27, 2020

WooCommerce Vs PrestaShop: What To Choose For Your Online Store?

Facebook Twitter LinkedIn Pinterest Shares To have a successful online business site you need to choose the right shopping cart software first.  But choosing the right platform might be a bit tricky because there is a wide range of solutions to choose from.

Pin It on Pinterest