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


Landing Page Vs Home Page

May 22, 2020

Landing Page Vs Home Page: What is the Difference?

Facebook Twitter LinkedIn Pinterest Shares The landing page and a Home page are quite confusing terms for most of the people out there. Many of you think they both are the same thing with different names. Completely wrong. Both terms are different from each other.

How to Build User Trust to Get Conversions on WordPress Site

May 18, 2020

How To Build User Trust To Get Conversions On WordPress Site

Facebook Twitter LinkedIn Pinterest Shares Gaining user’s trust is the very first step you need to take for the success of your website. New users may be wary of scams and feel hesitant to make any purchase or provide personal information in contact forms. However, your WordPress website plays a crucial role in building trust

Pin It on Pinterest