Support » Requests and Feedback » Search result in order of relevance, not in order of dates

  • I’m not sure, if this is the rights place to post a feature request for WordPress. I think, it should be implemented in WordPress itselfs, not in a plugin.

    I believe this, since it’s very simple and a lot of users would be interested about it.

    The request: An option to sort the search result in order or relevance, not in order of publish date, like right now.

    To implement this feature, a MySQL feature can be used, which MySQL support since several years.

    The SQL query of the search request has to be changed and a FULLTEXT index has to be added to the wp_posts table. With this changes, WordPress lists the results in order of relevance.

    To demonstrate, what I’m asking about, I realized it as filters. I hope, it’s Ok to post it here:

    add_filter( 'posts_fields', 'nw_posts_fields', 10, 2 );
    function nw_posts_fields( $fields, $query ) {
      global $wpdb;
      if ( is_search() )
        $fields .= ', MATCH (' . $wpdb->posts . '.post_title) AGAINST (\'' . $wpdb->_escape($query->query['s']) . '\') * 10'
          . ' + MATCH (' . $wpdb->posts . '.post_excerpt) AGAINST (\'' . $wpdb->_escape($query->query['s']) . '\') * 5'
          . ' + MATCH (' . $wpdb->posts . '.post_content) AGAINST (\'' . $wpdb->_escape($query->query['s']) . '\') * 1 AS score';
      return $fields;
    add_filter( 'posts_search', 'nw_posts_search', 10, 2 );
    function nw_posts_search( $fields, $query ) {
      global $wpdb;
      if ( is_search() )
        return ' AND (MATCH (' . $wpdb->posts . '.post_title) AGAINST (\'' . $wpdb->_escape($query->query['s']) . '\')'
          . ' OR MATCH (' . $wpdb->posts . '.post_excerpt) AGAINST (\'' . $wpdb->_escape($query->query['s']) . '\')'
          . ' OR MATCH (' . $wpdb->posts . '.post_content) AGAINST (\'' . $wpdb->_escape($query->query['s']) . '\'))';
        return $fields;
    add_filter( 'posts_search_orderby', 'nw_posts_search_orderby', 10, 2 );
    function nw_posts_search_orderby( $orderby, $query ) {
      global $wpdb;
      if ( is_search() )
        if ( $orderby )
          $orderby = 'score DESC, ' . $orderby;
          $orderby = 'score DESC';
      return $orderby;

    Additional, the FULLTEXT indices has to be added. But I believe the developer of WordPress will know, how to do this. So I don’t post it here.

    The advantage of this solution is not only the sort order. Addition, would be possible to add the FULLTEXT BOOLEAN MODE of MySQL, which offers further features like use wildcards like “WordPr*” or place an “+” or “-” in front of a word to make sure it’s present or not inside the related post. A detailed description of the possible additional feature you can find

    I’m not sure and I didn’t validated it, but I believe the search will be much faster by using an index.

    Indeed there is not advantage, without an disvantage: Adding FULLTEXT indices takes a lot of database storage memory.

    For older MySQL servers, the FULLTEXT is available for the MyISAM engine only. But by default WordPress uses the InnoDB engine.

    I suggest the following options inside the WordPress admin panel

    [X] Use search index (FULLTEXT INDEX)
    __(_) Order about dates
    __(X) Order in relevance

    __(_) Use standard search
    __(X) Use extended search options (BOOLEAN MODE)

    If my further assistance is welcome, please ask me about. I would also implement it completely into the WordPress core code, if this should be wanted.

    If this is not the right place for this feature request, maybe somebody can forward it or let me know, where I could place it instead of it.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.