0

我想输出一个指南列表,其中每个指南都有投票和评分。所以我想按评分和投票对指南列表进行排序。

例子:

Guide Name      Rating    Votes
Guide1            10        3
Guide2            10        2
Guide3            10        1
Guide4            9         6
Guide5            9         2

所以我必须用 2 个 meta_key 来订购指南

omvp_vote_rating and omvp_total_vote

我已经尝试了很多方法来让它工作,但还没有得到它。有一种方法我几乎可以让它工作,但问题是我无法通过 meta_key 订购它,我只能通过普通字段订购它comment_count, modified, date, rand (random), title ... 所以这是我的代码

$args = array(
        'post_type' => 'guides',
        'meta_key' => 'omvp_vote_rating',
        'post_status' => 'publish', 
        'posts_per_page' => 20,
        'meta_query' => array(
            array(
                'key' => 'omvp_vote_rating',
                'compare' => '>=',
                'value' => 0
            ),
            array(
                'key' => 'omvp_total_vote',
                'compare' => '>=',
                'value' => 0
            ),
        ),
        'orderby'    => array(
            'title' => 'ASC', //<- This is working
            'omvp_total_vote' => 'DESC' //<- This is not working
        ),
    );
$wp_query = new WP_Query( $args );
4

2 回答 2

2

您可以改为通过自定义选择查询来做到这一点。

$query = "SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta AS rating ON(
$wpdb->posts.ID = rating.post_id
AND rating.meta_key = 'omvp_vote_rating'
)
LEFT JOIN $wpdb->postmeta AS vote ON(
$wpdb->posts.ID = vote.post_id
AND vote.meta_key = 'omvp_total_vote'
)
WHERE $wpdb->term_taxonomy.term_id = 3

AND $wpdb->posts.post_status = 'publish'
ORDER BY rating.meta_value, vote.meta_value ASC LIMIT 0,20"

$posts = $wpdb->get_results($query, object);
于 2016-01-21T11:43:26.983 回答
0

这是可能的。

尝试这个:

'order'     => 'DESC',
'orderby'   => 'omvp_vote_rating omvp_total_vote'

这将根据omvp_vote_ratingomvp_total_vote对所有指南进行降序排序。

有关更多参考,请参阅wp_queryorder中的和orderby参数。

于 2016-01-21T07:58:50.097 回答