/** Relevanssi customization by max **/
add_filter('relevanssi_hits_filter', 'order_the_results');
function order_the_results($hits) {
global $wp_query;
switch ($wp_query->query_vars['orderby']) {
case 'likes':
$likes = array();
foreach ($hits[0] as $hit) {
$likecount = get_post_meta($hit->ID, '_likes', true);
if (!isset($likes[$likecount])) $likes[$likecount] = array();
array_push($likes[$likecount], $hit);
}
if ($wp_query->query_vars['order'] == 'asc') {
ksort($likes);
} else {
krsort($likes);
}
$sorted_hits = array();
foreach ($likes as $likecount => $year_hits) {
$sorted_hits = array_merge($sorted_hits, $year_hits);
}
$hits[0] = $sorted_hits;
break;
case 'comments':
$comments = array();
foreach ($hits[0] as $hit) {
$commentcount = wp_count_comments($hit->ID)->total_comments;
if (!isset($comments[$commentcount])) $comments[$commentcount] = array();
array_push($comments[$commentcount], $hit);
}
if ($wp_query->query_vars['order'] == 'asc') {
ksort($comments);
} else {
krsort($comments);
}
$sorted_hits = array();
foreach ($comments as $commentcount => $year_hits) {
$sorted_hits = array_merge($sorted_hits, $year_hits);
}
$hits[0] = $sorted_hits;
break;
case 'relevance':
//do nothing
break;
}
return $hits;
}