10

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.

  • 2
    That's an awful broad question for this site -- see the what to ask & what not to ask sections. I did a couple quick Google searches, and came across this: Make WordPress' Search Suck Less (it's from '08, though, so YMMV). As for influencing functionality without plugins -- If you're allergic to others' plugins, you're probably going to have to write one of your own. Plugins simply affect how WP functions, and that's what you're trying to do. Right? – Pat J Sep 27 '13 at 16:09
  • 1
    Thanks, it's not that I wan't to avoid using others' plugins, more the fact that I want to avoid 'plugin sprawl', i.e. "Oh, I'll have a plugin for this and that" and before you know it, you've got 30 of them running. – chrisd Sep 30 '13 at 13:33
  • I understand and sympathize. I run a number of plugins, but I've written a bunch of them. – Pat J Sep 30 '13 at 14:32
10

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.

  • 4
    Funny you should say that…Nacin committed code to sort by relevance about an hour ago. In fact, probably the same time you were writing your answer. :) core.trac.wordpress.org/changeset/25632 – Dave Ross Sep 27 '13 at 18:04
  • For big/popular blogs, searching all the content is pretty unrealistic. What's the recommended way to speed this up without just deleting the " OR (...)" from query.php? Should I submit a new question or can you expand your answer? – PJ Brunet Jan 24 '14 at 4:03
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.