如何反向排序这个数组,WordPress自定义搜索过滤器

时间:2012-09-04 08:28:02

标签: wordpress ksort

我正在使用WordPress中的自定义过滤器,只需要帮助按相反顺序对此数组进行排序:

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;
}

我对ksort不太熟悉,但它似乎没有ASC / DESC选项。

1 个答案:

答案 0 :(得分:6)

krsort()

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