If you want all the searches to return posts by date, just change the setting in the Relevanssi settings page.
If you want only some searches sorted by date, you can use the orderby parameter. Set it to post_date.
For example, you could have a dropdown like this on your search form:
<select name="orderby">
<option value="post_date">Date</option>
<option value="relevance">Relevance</option>
</select>
Thanks for your answer Mikko.
I think I wasn´t clear. I need to order them by the date in which each book was published (ACF field), not the date in which each book was posted.
That is:
Title: XXXX
Author: XXXX
Date: 2016
Thanks for your help
Change the select to
<select name="orderby">
<option value="publication_date">Date</option>
<option value="relevance">Relevance</option>
</select>
and add
add_filter( 'relevanssi_orderby', 'publication_date_orderby' );
function publication_date_orderby( $orderby ) {
if ( 'publication_date' === $orderby ) {
global $wp_query;
$orderby = 'meta_value_num';
$wp_query->query_vars['meta_key'] = 'XXX';
}
return $orderby;
}
to your functions.php. Just change “XXX” to the name of the date field. There’s a small catch, though: this doesn’t work with the current version of Relevanssi. It will work with the next version, and also with the current version, if you replace the lib/sorting.php file with this one: https://github.com/msaari/relevanssi/blob/master/lib/sorting.php