How does WordPress search work behind the scenes?

All we need is an easy explanation of the problem, so here it is.

I am using WordPress search to search a range of custom post types and allow the user to drill-down into each individual post type.

However I’m finding that WordPress is not returning what I’d deem to be the ‘best’ matches first. For example, if I search for Microsoft, pages that contain the term irregularly are still returned before pages that regularly use the term.

Basically I’m trying to find some information on how WordPress ranks pages and determines relevance and if I can influence this without having to install any plugins.

Any help much appreciated.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

EDIT – The current version of WordPress supports relevance, so this answer is no longer accurate.

There’s no concept of rank or relevance, it’s just a simple LIKE query on the post title and content:

($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')

You can use the posts_search filter to modify or completely replace the search query with your own. From /wp-includes/query.php:

// Allow plugins to contextually add/remove/modify the search section of the database query
$search = apply_filters_ref_array('posts_search', array( $search, &$this ) );

Also see the available query filters on the WP_Query Codex page.

Method 2

Since WordPress 3.7 (October 2013), search results are sorted by the following criteria (see ticket 7394):

  • Full sentence matches in post titles.
  • All search terms in post titles.
  • Any search terms in post titles.
  • Full sentence matches in post content.

Each section and any remaining posts are then sorted by date.

There are also new filters:

  • wp_search_stopwords, to filter stop words ignored in WHERE.
  • posts_search_orderby, to filter the ORDER BY when ordering search results.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply