Improving Native WordPress Search

Share this article

Improving Native WordPress Search

This article on WordPress search was originally published by Torque Magazine, and is reproduced here with permission.

If you have ever worked on a WordPress site with a massive amount of posts and pages, you might have noticed something about the WordPress search: it doesn’t perform well. In this article, we dive into why the default WordPress search doesn’t scale and some ways to make it better.

How WordPress Searches

By default, the native WordPress search performs a query on your database that looks something like this:

A native WordPress database search query

Let’s break down this query a little bit.

  • First, we are using SQL_CALC_FOUND_ROWS on the wp_posts table. This says, if there were no limit (coming later), how many results would the following query turn up?
  • Then we search through the post_title, post_excerpt, and post_content columns for the search term (in this example I searched “tech”).
  • And we ensure that the content returned is a post, page, or attachment (media).
  • We check to make sure the status is “public”–not deleted or private (if you are a logged-in user it will also search only your own private posts).
  • We order the results by matching title, descending, or post date, descending.
  • Last, we limit the results to 10, paginating the rest of the results.

That’s a whole lot of checks WordPress runs through just to return search results. And every time you iterate through wp_posts to perform each check, MySQL is processing all rows in wp_posts. When you have hundreds of thousands of rows in the wp_posts table, this query can get ugly really quick. Here are some benchmarks from a few of my sites:

Bar graph showing query times per number of posts

On sites with a very large number of posts, the query took over 20 seconds! This is not a scalable search option for sites with high traffic.

Problem Identification

Knowing what we now know about the default search behavior, it’s important to call out why this might not be ideal for some websites.

  • WordPress only searches the “post_title,” “post_content,” and “post_excerpt” fields for your search terms. It also only presents “post,” “page,” and “attachment” post types. For users with custom post types, custom fields, or plugins like WooCommerce that add different page/post types, this can be problematic: these items will not appear in search results.
  • The query used by WordPress search performs very poorly at over 100,000 posts. For news sites or media sites with more than 100,000 posts, a search could take several seconds to perform.
  • The poor performance on the search query could cause server performance issues if your site receives a fair amount of search traffic.

Improving on WordPress Native Search

So now that we know WordPress native search isn’t an option for many sites, we can explore search solutions. Based on our problem identification above, our qualifications for a search solution include:

  • Search tools that will search all content or customized sets of content, not limited to the post types or post fields defined by default.
  • Search tools that perform well when presented with large data sets.
  • Search tools that will not cause poor server performance when presented with high traffic.

Below we will explore several solutions, including enterprise-grade external services and WordPress plugins.

Algolia Search

Algolia is an enterprise-grade search solution in which your posts and content are indexed offsite and returns results to your users. Its feature set includes fuzzy search, geolocation search, multi-language support, and search with synonyms.

Comparing search performance on a site with thousands of posts, there are some clear benefits with Algolia. Below is the performance of the default WordPress search:

DevTool view of the default WordPress search performance

Notice the search took 7.43 seconds using the default WordPress search. However, after implementing Algolia search the results were nearly instant:

DevTool view of Algolia search performance

The Algolia results were about 15x faster than standard WordPress search! That is a massive improvement in performance. Not to mention, if the site supports a large amount of traffic with concurrent searches, it will not cause strain on the server. Offloading searches to an external service that is specifically optimized for searching is a big win.

Elasticsearch

ElasticPress is a product made by Elastic, which offloads your searching to your own Elasticsearch server instance. This search tool is unique in that you can choose where to host your Elasticsearch instance: your local desktop, dedicated servers, or even on the same server where your WordPress website is hosted. And with the ElasticPress plugin, you can easily connect this instance to your website for use.

ElasticPress + ElasticSearch is a winning combination that provides excellent search features like:

  • Including WooCommerce products in search results
  • Display related posts without expensive, slow queries
  • Choose to index all, some, or only select content on your website
  • Automatically complete search suggestions as users type

Because the search is offloaded to a service optimized for search, the performance impact for sites with many thousands of posts is boosted as well.

Amazon CloudSearch

CloudSearch

The CloudSearch service from Amazon is a viable option in terms of power, performance, and scalability. Amazon uses a “pay-as-you-go” payment service and this makes CloudSearch an inexpensive enterprise-grade solution. You can customize which items get indexed in the setup process as well, but the learning curve for this product is steep. While some like Lift have created WordPress plugins integrating CloudSearch, they are not widely used or often updated, and may not be as reliable. This means that chances are you will be developing your own plugin implementation of Amazon CloudSearch. You can use their Developer Guide for CloudSearch to get started.

Relevanssi

Relevanssi

Relevanssi is a WordPress plugin that optimizes the native WordPress search “in-house.” That is, it searches your own database on your own server, just with better results. Some of the key benefits this plugin offers, compared to the default WordPress search:

  • Fuzzy searches (include partial matches)
  • Sort by relevance, not by date
  • Select whether the results should match all words, or just one of the words
  • Search all content including comments, categories, tags, and custom fields
  • Excerpts display and highlight where the match occurred for context

The one aspect Relevanssi does not necessarily improve upon is the speed or scalability of the search function. In this way, Relevanssi is ideal for users who are frustrated with the type of results WordPress search produces. It is not ideal for users who are seeking performance improvements in search.

SearchWP

Last but not least, SearchWP is another paid search solution for WordPress. As with Relevanssi, this plugin extends the search to include better results but makes no claim to improve search performance. SearchWP has extensive configuration options to include exactly what you want in search results. Search things like: eCommerce products, PDFs and Media, Custom Posts and Fields, Categories and Tags, and Shortcode output. Beyond the additional search vectors, SearchWP offers keyword stemming as well. So if your end user searches for a related keyword, they will get content from related keywords in the results.

SearchWP also goes the extra mile in terms of logging searches and providing insights on search traffic. This can help inform your content strategy in ways that make your content easier to find.

Choosing the Right Search Solution

In the end, the search solution you choose will depend upon your budget and upon which problems are the highest priority for your team. If performance gains are your primary concern, Algolia and Elasticsearch are the clear winners. However, if you just want to see better search results, Relevanssi and SearchWP are simple yet powerful options. And if you want a cost-effective way to completely customize the way your website searches in a customized plugin, Amazon CloudSearch could be the right solution. You can use the descriptions above to identify your priorities, and the proper solution to fit that need. As always, be sure to test any new integration or plugin in a staging or development environment first! This will allow you the freedom to test your new search implementation in a non-user-impacting way.

How can I improve the speed of my WordPress search?

Speed is a crucial factor in improving your WordPress search. You can enhance the speed by using a caching plugin, which stores your website data and serves it to users faster. Another way is by using a Content Delivery Network (CDN) that stores your website on servers worldwide, reducing the distance data has to travel to reach users. Lastly, consider using a search plugin that indexes your website content for faster retrieval.

What are some recommended plugins for improving WordPress search?

There are several plugins available that can significantly improve your WordPress search functionality. Some of the most popular ones include Relevanssi, SearchWP, and ElasticPress. These plugins offer features like fuzzy matching, keyword stemming, and the ability to search custom fields, among others.

How can I customize the search results page in WordPress?

Customizing the search results page can be done by creating a search.php file in your theme directory. This file will be used by WordPress to display search results. You can customize this file to change the layout, add or remove information, and more. Alternatively, you can use a plugin like Search & Filter or FacetWP to customize your search results page without coding.

Can I improve WordPress search without a plugin?

Yes, you can improve WordPress search without a plugin by modifying your theme’s functions.php file. However, this requires a good understanding of PHP and WordPress’s inner workings. If you’re not comfortable with coding, it’s recommended to use a plugin.

How can I make my WordPress search more accurate?

To make your WordPress search more accurate, consider using a search plugin that supports advanced search features. These features may include the ability to search custom fields, search by category or tag, and more. Additionally, you can improve search accuracy by regularly updating and optimizing your website content.

How can I add a search bar to my WordPress site?

Adding a search bar to your WordPress site can be done by going to Appearance > Widgets in your WordPress dashboard. From there, you can drag the “Search” widget to any widget area on your site. Alternatively, you can add a search bar using a plugin or by modifying your theme’s code.

How can I exclude certain content from WordPress search results?

Excluding certain content from WordPress search results can be done by using a plugin like Search Exclude or Relevanssi. These plugins allow you to specify which posts, pages, or custom post types should be excluded from search results. Alternatively, you can exclude content by modifying your theme’s functions.php file.

How can I improve the design of my WordPress search bar?

Improving the design of your WordPress search bar can be done by modifying your theme’s CSS. This allows you to change the size, color, and style of the search bar. Alternatively, you can use a plugin like WP Search with Algolia, which offers a customizable search bar design.

Can I add filters to my WordPress search?

Yes, you can add filters to your WordPress search by using a plugin like Search & Filter or FacetWP. These plugins allow users to filter search results by category, tag, date, and more. Adding filters can greatly improve the user experience on your site.

How can I track what users are searching for on my WordPress site?

Tracking what users are searching for on your WordPress site can be done by using a plugin like Search Meter or Google Analytics. These tools allow you to see what search terms are being used, how often they’re being used, and more. This information can be valuable for understanding what your users are interested in and optimizing your site accordingly.

Janna HilfertyJanna Hilferty
View Author

Janna Hilferty is a Content Specialist at WP Engine. She loves both technical and free-form writing, hiking with her dog, and painting with all the colors of the wind. In her free time you can find her blogging, smoking a cigar, or watching cheesy documentaries.

searchWordPressWordPress searchWordPress-hubWordPress-tools
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week