WordPress is an amazing platform in a lot of aspects. However, one area where thereβs definitely room for improvement is its internal search. A lot of users agree that WordPressβ search feature doesnβt always deliver the best results. In practice, that means some of your siteβs visitors might be missing out on content theyβd be interested in.
Fortunately, WordPress is nothing if not customizable. There are plenty of ways to improve your WordPress search results, such as excluding irrelevant pages from them. In this article, weβll talk more about WordPressβ default search feature, then weβll teach you two ways to exclude content from it.
Letβs get our search on!
A Quick Look at WordPressβ Default Search Feature
When you set up a new WordPress website, it will come equipped with a default search bar. You can use this feature as you would any other: you enter a query and get a list of relevant results. Unfortunately, the results you get arenβt always the most accurate.
Sure, weβre all a bit spoiled by Google, but even if you go in with low expectations, youβll often find that the default search donβt meet them. For example, when you set up WordPress for the first time, the platform will add a βHello world!β post whose aim is to help you get acquainted with the platformβs editor.
If we use the search feature to look for the term βhelloβ, sure enough, it returns that post. However, hereβs what happens if we try βhello dollyβ:
The problem is WordPress will always look for posts and pages that include all your search terms by default. In other words, sometimes it wonβt return any useful search results unless your visitors try multiple times using different keywords. Thatβs not something you want to force on your users since most of them may give up after the first try.
As if that wasnβt enough, WordPressβ search module doesnβt look through tags or custom fields. In other words, itβs all posts and pages, and nothing else.
The good news is you can improve WordPressβ search functionality using plugins and by tweaking its code. However, that can be a bit overkill unless your website has a massive library of content. In our experience, you can also get improved results by telling WordPress which pages it shouldnβt crawl, so they donβt clutter up your search results. Letβs talk about how you can improve your WordPress search results.
Why You Might Want to Exclude Certain Content From Your Search Results
There are several reasons why you might want to exclude specific pages from your WordPress search results. Some of these include:
- Youβre publishing private content. If youβre publishing posts you only want specific users to see, you wonβt want them to show up on your siteβs search results.
- To prevent irrelevant pages from showing up. For example, you might not want your homepage or your author pages to show up on your search results. With a little tweaking, you can exclude all of them.
Ultimately, excluding irrelevant pages from your search results should help visitors find the content they want faster. Plus, itβs pretty easy to implement this change in WordPress with the right tools.
How to Exclude Specific Pages From Your WordPress Search Results (2 Methods)
In this section, weβre going to cover two approaches to implementing this functionality into WordPress. If youβre a Divi user, you can improve your WordPress search results using your theme settings, which weβll show you how to do first. However, if youβre using another theme, you can also exclude pages from your search results, but youβll need to tweak a core file to do it.
Letβs get started!
1. If Youβre a Divi User, Use Diviβs Search Module
With Divi, you get full control over your WordPress search bar placement. The theme includes a Search module which you can add to any of your pages, like this:
With the Search module, you can customize many aspects of your search bar, including its design:
If you go to the moduleβs settings screen and open the Content tab, youβll find a section called Exceptions. There are three settings here, including options to exclude pages and/or posts altogether from your WordPress search results:
For more control over the posts you want WordPress to ignore, youβll need to use the Exclude Categories setting:
All you have to do is set up a category and assign it to the posts you donβt want to show up in your search results. Then, select that category under the Exclude Categories section and save your changes.
The only problem with this approach is, WordPress doesnβt enable you to organize pages using categories and tags as it does with posts. To add this functionality to your website, youβll need to use a plugin such as Category Tag Pages.
After enabling this plugin, youβll be able to create and assign categories and tags to pages. You can then apply your exclusion category to pages and set it in your Search moduleβs Exclude Categories section.
Now, when someone searches your website, the posts and pages in the excluded category wonβt appear, even if they include the search terms.
2. Tweak Your WordPress functions.php File
If youβre not using Divi )or you just want more control over which pages you exclude from your search results), you can always go the manual route. This process involves editing your themeβs functions.php file, though, so make sure that you create a backup of your website before you get started.
To get started, youβll need to connect to your website via File Transfer Protocol (FTP) using a client such as FileZilla. Once youβre in, navigate to the public_html/wp-content/themes directory. Inside, you should find folders for each of the themes installed on your website:
Look for the folder that corresponds with your active theme, which should be easy since they likely share the same name. Keep in mind, we always recommend using child themes for these types of modifications. In fact, you should be using a child theme if you plan to make almost any type of customization to your themeβs code.
Open your active themeβs folder and look for the functions.php file within. Right-click it and choose the View/Edit option if youβre using FileZilla:
This will open the file using your default text editor, enabling you to make changes to the code within. Once youβre in, there are two ways you can exclude specific pages from your search results. You can do it using either tags or categories (as we did with Diviβs Search module).
Hereβs an example of the code you need to add if you want to exclude specific categories from your search results:
function wpb_search_filter( $query ) { if ( $query->is_search && !is_admin() ) $query->set( 'cat','-5, -11' ); return $query; } add_filter( 'pre_get_posts', 'wpb_search_filter' )
Notice the line that says cat followed by two numbers. Each of those numbers represents a category ID. Finding a categoryβs ID is simple. All you have to do is go to your Categories tab for either your posts or pages (if youβre using the Categories Tag Pages plugin we mentioned earlier) and hit the Edit button.
Now pay attention to that pageβs URL. There should be a section that says category&tag_ID=X. Only instead of an X, youβll see a number, which is the ID you need to insert into the code we showed you earlier.
Keep in mind, you can include as many categories as you want within that code, or just a single one. Now letβs check out how that code changes if we want to filter pages using tags instead of categories:
function wpb_search_filter( $query ) { if ( $query->is_search && !is_admin() ) $query->set( 'tag','-5, -11' ); return $query; } add_filter( 'pre_get_posts', 'wpb_search_filter' );
The code is basically the same, except weβre replacing the term cat with tag. You can use either approach depending on your siteβs taxonomy. Just remember to save the changes to your functions.php file and youβre good to go!
Conclusion
There are few aspects of WordPress you canβt improve with a little elbow grease, and its search feature is no exception. By removing irrelevant pages from your search results, you can help visitors find the content they need faster. This may get them to spend more time on your site and walk away happier.
When youβre ready to get to work and improve your WordPress search results, there are two ways you can remove specific pages from them:
- Use Diviβs Search module to exclude the pages you want.
- Tweak your WordPress functions.php file.
Do you have any questions about how to improve your WordPress search results? Letβs talk about them in the comments section below!
Article thumbnail image by VectorKnight / shutterstock.com
What I really need is “include” and not “exclude”. For example, I want to have a search that only displays pages that have been marked with a certain category and all other categories are automatically excluded.
It is easier to select a few pages and “include” them than it is to “exclude” all other pages (a lot of them!).
Nevermind. Rebooting the server allowed it to start working.
I tried method 1) with Category Tag Pages, but Divi “Search > Content > Exceptions > Exclude Categories” does not offer any categories I created. In fact, it only offers the category called “Posts”.
Category Tag Pages Version 1.0
Divi Version 3.17.1
WordPress Version 4.9.8
Is Divi broken?
Thanks John, it is really useful for me, tweaking function.php looks quite easy.
You’re welcome, Gavin! While it can be easy, do be careful when working with WordPress’ core files. π
This is a great guide. I have been looking for a function like this, and didnt know it was possible in divi.
Thanks for info.
I’m really glad to hear you found it useful, Kristoffer! π
I have 2 questions
It seems that the search does not include the projects nor its categories
How can we create a custum resultpage with divi so that we can post the results on a page with the same layout as the site itself
I was hoping to find that out too.
Maybe to add to this. How can we create a support center or faq page with search function that filters out questions on the same page based on what has been searched without having to go the custom posttype and toolset route?
I was hoping to find that out too.
Maybe to add to this. How can we create a support center or faq page with search function that filters out questions on the same page based on what has been searched without having to go the custom posttype and toolset route?
Thank you for the information. But 1 question: I would like to exclude a page from the search results of Google etc. It should not be indexed (the reason: It’s a certain legal text which hat been copied to often without asking us).
Is there a possibility WITHOUT a plugin?
Thank you for any advice.
Hi Anna π
First of all, I’m very glad you liked the article. Secondly, you may find some useful information for excluding pages in this article: https://www.elegantthemes.com/blog/tips-tricks/how-to-stop-search-engines-from-indexing-specific-posts-and-pages-in-wordpress
@John Hughes: Thank you, I’ll check it. BTW: Divi is great!
May I also suggest the FREE plugin Search Exclude over at WordPress.org
That way you can exclude select pages or posts from search.
Thank you for the recommendation, Richard! π