relevanssi_sort_compare

apply_filters( 'relevanssi_sort_compare', string $compare, string $key )

This filter hook controls the comparison method for sorting.

Parameters

$compare
(string) The comparison method (string, date, number, filter). The default value is string. Unknown values also default to string.

$key
(string) The name of the field used for sorting.

More information

When Relevanssi sorts the posts it finds, it figures out which comparison method to use based on the data. In many cases, these are obvious: date fields are sorted as dates, numeric fields are sorted as numbers, and text fields are sorted alphabetically.

In the case of custom fields, Relevanssi cannot know which comparison method is correct. This filter hook lets you choose the comparison method. The default method is the string comparison, so you don’t have to specify that, but if you have a numeric field, you can let Relevanssi know:

add_filter( 'relevanssi_sort_compare', function( $compare, $key ) {
  if ( 'publication_year' === $key ) {
    $compare = 'number';
  }
  return $compare;
}, 10, 2 );

This function would set the comparison to number for the field publication_year. The possible comparison methods are: