phpwordpresssortingksort

how to reverse sort this array, WordPress custom search filter


I'm working on a custom filter in WordPress and just need help sorting this array in reverse order:

add_filter('relevanssi_hits_filter', 'order_the_results');

function order_the_results($hits) {
global $wp_query;
if ($wp_query->query_vars['orderby'] == '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);
    }
    ksort($likes);
    $sorted_hits = array();
    foreach ($likes as $likecount => $year_hits) {
        $sorted_hits = array_merge($sorted_hits, $year_hits);
    }
    $hits[0] = $sorted_hits;
}
return $hits;
}

I'm not too familiar with ksort, but it doesn't seem to have a ASC/DESC option.


Solution

  • krsort()

    if ($wp_query->query_vars['order'] == 'asc') {
          ksort($likes);
        } else {
          krsort($likes);
        }