Skip to content

Instantly share code, notes, and snippets.

@aburi
Created October 5, 2014 21:31
Show Gist options
  • Select an option

  • Save aburi/02101531bf828749fbd0 to your computer and use it in GitHub Desktop.

Select an option

Save aburi/02101531bf828749fbd0 to your computer and use it in GitHub Desktop.
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') {
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;
}
@azizultex
Copy link
Copy Markdown

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