How to improve WordPress search

How to improve WordPress search

Post author

Category

Tag

The default WordPress search functionality is certainly lacking in areas. Although changes were made in 2013 to improve it, there are still a few areas where WordPress could use some help. It is, however, relatively easy to improve WordPress search by adding a few pieces of code. Here, I’ll list some of the methods you could use to make WordPress search better.

Sort by relevance

In older versions of WordPress, search results were sorted by date and not much else. Because this is (at the very least) annoying for websites with a lot of posts, WordPress core introduced a patch that would change the way search results are sorted.

The changes are as followed:

Results with a full sentence match in the post title are listed first.

Search results that include all search terms in the title, but not a full sentence match, are listed next.

Results including any search terms in the title, but not all search terms or a full sentence match, are listed next.

Finally, search results that include a full sentence match in the post content come last. Within each group, results are further sorted chronologically by publication date.

Improving the interface

Excerpts in search results by WordPress are not exactly great. Unlike Google, the WordPress search omits emphasis of the keyword if it found matches. Luckily, you can alter parts of the search results and add this feature.

Preparing your theme

In your theme, look for the file that outputs the search results. In this example, it’s a file that I’ve created manually, called /template-parts/post/content-search.php in a Twenty Seventeen child theme. This file is a copy of content-excerpt.php that exists in the same directory.

Next, look for the file called search.php in the theme’s main directory and look for the following line of code:

get_template_part( 'template-parts/post/content', 'excerpt' );

Change this to the following:

get_template_part( 'template-parts/post/content', 'search' );

By making these changes, you’ll ensure that WordPress will use your custom template instead of the default one. Time to add the actual code that will be doing the emphasizing!

What this function does, is taking the passed content and emphasize every occurrence of the word(s) passed in $search_query and return the text. The class that was added to the <strong> tag can be used to further style the end result (if you want to).

Adding emphasis in the title

Now that we’ve gone through the steps to setup your custom template parts adding emphasis in the title is relatively easy to do.

Go into your newly created content-search.php and find the line that looks like:

Adding emphasis in the excerpt

You’d expect that adding emphasis to the excerpt can’t be much harder than adding it to the title. Sadly, this is not the case. With excerpts, WordPress automatically concatenates a “Continue reading” link to the end. You’d be fine as long as the search phrase doesn’t exist in the slug of the post, but most of the time if you’re looking for specific keywords, it will be present in the slug. This results in a broken “Continue reading” link.

To overcome this, you’ll have to temporarily overrule some default WordPress behavior.

Sadly, there’s no elegant alternative for this. Hopefully, someday, a filter will be created that can be called instead of having to overrule large portions of the trim function.

Tracking searches

There are a few options to track the search queries that visitors have entered, but we recommend using Google Analytics for this. To get started with tracking searches, please go through the following steps:

Login to Google Analytics.

Click on Admin (gear on the bottom-left).

Under View, click on View Settings

Scroll down until you find the Site Search Tracking toggle and turn it on.

In the Query Parameter field, enter s. This is the default query parameter that is added by WordPress when using the search function.

Click Save

If your website is heavily dependent on categories and allows users to use them to refine their searches, Google Analytics gives you the ability to add tracking on this too. For more information on this subject, you can read Google’s documentation on search tracking in this article.

Alternatives

Luckily, there are some alternatives to choose from when you want to improve the search function on your website. As pointed out in the comments, the plugin Relevanssi tackles most issues with the search and helps you get started quickly and easily.

If your website has grown a lot and you want to supercharge your search even more, it might be wise to look at a few, more complex alternatives. One that we use at Yoast is Algolia. This platform contains a ton of features to make search even better. Some features are: Typo-tolerance, support for synonyms, filters and support for 100+ languages. It also includes integrations with WordPress!

Another alternative is Amazon CloudSearch. It offers similar features to Algolia, and you can enable autoscaling if you think your website needs it. However, ACS does not provide you with an integration out of the box, so you’ll have to write your implementation or look for a WordPress plugin in the Plugin Directory. At the time of writing, there are only two plugins present; CloudSearch and Lift.

Yes, you can improve WordPress search

As you could read, the WordPress search has improved over the years. Despite this, it still lacks in some aspects. Luckily you can improve it by adding some extra code in your child theme or take it to the next level by using external services such as Algolia and Amazon CloudSearch. Good luck!

17 Responses to How to improve WordPress search

I’ve been using it for years for some of my money sites. Really happy with the great results. thanks.

By upmusicon 1 October, 2017

Thank you for a good article
Really, WordPress is poorly searched
It’s better to provide more search options

By P Viveenon 1 October, 2017

Thanks for the suggestion for the part in search.php. Just what I needed

By ulf__induo.comon 28 September, 2017

Thanks for your great post and thoughts. I do also recommend Relevanssi, great plugin and awesome support. Have been using it for many years and it is a quick way to implement many of these functions.

By Jimmy Comackon 2 October, 2017

Thanks for your kind words! For some reason, Relevanssi didn’t show up during my research. I’ll make sure to add it to the Alternatives section.

By Jason Antrosioon 28 September, 2017

Thank you for a great post and great suggestions. I’m curious as to why you don’t recommend Google’s internal search. Is that not a good thing anymore?

By Jimmy Comackon 2 October, 2017

Hey there, Jason.

Google discontinued this feature back in April. Hope this answers your question.

By Jason Antrosioon 5 October, 2017

Whoops, I’ve been out of it. Thank you for the update!

By jadeconcepton 28 September, 2017

Or, if you don’t like the idea of outsourcing your search for a pricey ongoing monthly premium, and/or you don’t need a huge powerhouse behind your search functionality, there’s Relevanssi plugin. It’s been around for a long time, it has pretty much all of the features you described above (spelling error correction in the premium version), the free version is already excellent, and the premium version is very reasonably priced…https://wordpress.org/plugins/relevanssi/

I’ve been using it for years on any site that requires reasonable search capabilities (yes, native WP search is awful), and I’ve always been happy with the results.

By Jimmy Comackon 2 October, 2017

Thanks for your reply. I added it to the top of the Alternatives section.

By phil001on 28 September, 2017

Great article… but what happens if the Theme is updated? Do these changes then disappear and have to be applied again?
Also, is there a working example of what the search is like with these changes applied?
Thanks

By apermoon 28 September, 2017

Hi Phil001, like with any other change you want to do this in a child theme.

I’m going to give this a try. I do have one other question. Is there a way to get WordPress search to query an external website and return results? I have a college catalog that exists in another location and would like to make it searchable using the search on my main website.

In this post, we'll give you a step by step instruction on how to find the data that will help you track your SEO using Google Analytics. The videos in this post (without sound so you can watch them everywhere) show you exactly which steps to take to start tracking your SEO.