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!


Source: Elegantthemes