Relvanssi - sorting by meta value & hits filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') { | |
ksort($dates); | |
} else { | |
krsort($dates); | |
} | |
$sorted_hits = array(); | |
foreach ($dates as $startdate => $year_hits) { | |
$sorted_hits = array_merge($sorted_hits, $year_hits); | |
} | |
$hits[0] = $sorted_hits; | |
break; | |
case 'relevance': | |
//do nothing | |
break; | |
} | |
return $hits; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the note. This saved my day!