Core WordPress search functionality
The standard search functionality of a WordPress installation makes use of MySQL. MySQL supports various full-text search options.
The WordPress Search box will result in a request that uses the s query argument: /?s=onion
for example. This translates in code to a WPDB object where is_search()
is true
, and the actual DB query uses very default SQL:
SELECT wp_posts.ID
FROM wp_posts
WHERE 1 = 1 AND
( ( ( wp_posts.post_title LIKE '%onion%' ) OR
( wp_posts.post_excerpt LIKE '%onion%' ) OR
( wp_posts.post_content LIKE '%onion%' ) ) )
AND wp_posts.post_type IN ( 'post', 'page' )
AND ( wp_posts.post_status = 'published' )
ORDER BY wp_posts.post_title LIKE '%onion%' DESC, wp_posts.post_date DESC
LIMIT 10
The result is fairly basic, matching as many posts via the three columns post_title
, post_excerpt
, and post_content
, and ranking by post_title
matches and then post_date
. With the WPDB class’s hooks and filters, search can be further customized and optimized to give different weights or to search beyond those 3 columns, such as including comments.
A default installation of WordPress does not create any FULLTEXT
indexes, and a small database (a relatively simple site) will still perform adequately for full-text searches without such an index.
Adding a FULLTEXT database index for each of the searched columns is possible, but this often impacts other aspects of database performance. We do not recommend adding these indexes on WordPress VIP.
Typically this default search behavior has limitations with respect to scaling: as the number of posts in a site increases, more resources are needed to maintain the indexes, and a search request can take several seconds. Performance optimization is usually needed to enable a large site or a site with frequent content additions to handle search traffic well.
Because the WPDB class is flexible, and because other search engine functionality has become more readily available and well-supported, many solutions that improve and/or speed up search actually offload the search queries to an API that communicates with a separate search engine, such as Elasticsearch.
Last updated: March 13, 2024