• Resolved uisneach

    (@uisneach)


    I have my Relevanssi Premium settings set to use Relevance for the sort order.
    However, there are some posts on my website that have the exact same title and they differ mainly by date. The posts show in the search result sorted by the oldest first.
    I would prefer that when there is a tie with the Title that they sort by newest first.

    So I implemented the code on this page in my functions.php, but changing the order to DESC so it would sort by newest first as a secondary sort
    https://www.relevanssi.com/knowledge-base/ordering-search-results-date/

    But for some reason it doesn’t change the sort order. Is there something else I am missing?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Relevanssi Premium support is here. However, since this isn’t related to Premium features, I can answer here as well.

    If you have Relevanssi set to sort posts by relevance, but you’re still seeing posts sorted by date, oldest first, that would suggest that something is overriding the Relevanssi settings. In that case adjusting the Relevanssi sorting order may not do anything, since the override is still overriding.

    This should work:

    add_filter( 'relevanssi_modify_wp_query', 'rlv_secondary_sort' );
    function rlv_secondary_sort( $query ) {
      $query->set( 'orderby', array( 'relevance' => 'desc', 'post_date' => 'desc' );
      return $query;
    }

    This would sort first by relevance descending, then by post_date descending. However, if this does nothing either, then there’s definitely something that overrides the settings. If you try the Relevanssi admin search (Dashboard > Admin search), does that sort the results correctly?

    Thread Starter uisneach

    (@uisneach)

    Hi Mikko,
    Thanks for the response. I tried your code snippet but it didn’t make any impact.

    I tried running the same search in the Dashboard –> Admin Search and there it did work.

    I am searching for a keyword in a meta field if that makes any difference, and the meta fields are being indexed.

    On the frontend, I am running a custom search in PHP to use WP_QUERY with this array passed as arguments.
    Array ( [s] => $keyword [relevanssi] => 1 [paged] => 1 )

    I can get the correct sort order to show if I actually add to that array an ‘orderby’ =>’post_date’ and an ‘order’ => ‘desc’, but then obviously I lose the default relevance sort.

    On the backend the search output is

    post_types: any
    s: the keyword
    relevanssi_admin_search: 1
    orderby: desc desc
    operator: AND
    posts_per_page: -1

    It does seem like something else on the frontend is interfering. I suppose I would have to disable all plugins to see what that might be.

    Plugin Author Mikko Saari

    (@msaari)

    Ah, if you’re using custom search, that doesn’t use relevanssi_modify_wp_query (because there’s no point, as you can modify the WP_Query directly).

    Set the orderby in the query to array( 'relevance' => 'desc', 'post_date' => 'desc' ), that way you get two levels, relevance first and post_date second. (In practise this has less effect than you probably expect, because exact ties in relevance are fairly rare.)

    Thread Starter uisneach

    (@uisneach)

    Awesome Mikko – that solved it for me.

    Much appreciated!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Secondary sort order for results’ is closed to new replies.