Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Relvanssi - sorting by meta value & hits filter
add_filter('relevanssi_modify_wp_query', 'start_date_filter');
function start_date_filter($query) {
$query->set('orderby', 'departure');
$query->set('order', 'asc');
return $query;
// Sort by meta field
add_filter('relevanssi_hits_filter', 'order_the_results');
function order_the_results($hits) {
global $wp_query;
switch ( $wp_query->query_vars['orderby'] ) {
case 'departure':
$dates = array();
foreach ($hits[0] as $hit) {
$startdate = get_post_meta( $hit->ID, 'start_date', true );
if ( !isset( $dates[$startdate] ) ) $dates[$startdate] = array();
array_push($dates[$startdate], $hit);
if ($wp_query->query_vars['order'] == 'asc') {
} else {
$sorted_hits = array();
foreach ($dates as $startdate => $year_hits) {
$sorted_hits = array_merge($sorted_hits, $year_hits);
$hits[0] = $sorted_hits;
case 'relevance':
//do nothing
return $hits;

This comment has been minimized.

Copy link

@azizultex azizultex commented Nov 22, 2015

Thanks for the note. This saved my day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.