How to Exclude Specific Pages From Your WordPress Search Results

Posted on August 22, 2018 by in Tips & Tricks | 15 comments

How to Exclude Specific Pages From Your WordPress Search Results

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:

  1. Use Divi’s Search module to exclude the pages you want.
  2. 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

Check Out These Related Posts

Splice Video Editor: An Overview and Review

Splice Video Editor: An Overview and Review

Posted on May 7, 2019 by in Tips & Tricks

Video is a valuable form of content for social media. Unfortunately, creating quality videos is usually a long process that involves moving mobile footage to a desktop app for editing. However, mobile editing is on the rise. Apps such as Splice Video Editor make it possible to efficiently create...

View Full Post

15 Comments

  1. May I also suggest the FREE plugin Search Exclude over at WordPress.org

    That way you can exclude select pages or posts from search.

    • John Hughes

      Thank you for the recommendation, Richard! πŸ™‚

  2. 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.

  3. 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?

  4. 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.

    • John Hughes

      I’m really glad to hear you found it useful, Kristoffer! πŸ™‚

  5. Thanks John, it is really useful for me, tweaking function.php looks quite easy.

    • John Hughes

      You’re welcome, Gavin! While it can be easy, do be careful when working with WordPress’ core files. πŸ™‚

  6. 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?

  7. Nevermind. Rebooting the server allowed it to start working.

  8. 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!).

Join To Download Today

Pin It on Pinterest