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.

有帮助吗?

解决方案

krsort()

if ($wp_query->query_vars['order'] == 'asc') {
      ksort($likes);
    } else {
      krsort($likes);
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top