【问题标题】:how to reverse sort this array, WordPress custom search filter如何反向排序这个数组,WordPress 自定义搜索过滤器
【发布时间】:2012-09-04 08:28:02
【问题描述】:

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

【问题讨论】:

    标签: wordpress ksort


    【解决方案1】:

    krsort()

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-02
      • 2014-04-30
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      相关资源
      最近更新 更多