1

我正在使用 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 选项。

4

1 回答 1

6

krsort()

if ($wp_query->query_vars['order'] == 'asc') {
      ksort($likes);
    } else {
      krsort($likes);
    }
于 2012-09-04T08:48:50.027 回答