wordpress按meta_value排序查询,然后按post_date排序

时间:2014-03-01 22:01:18

标签: wordpress date

我正在使用以下查询(mytheme / function.php)按自定义字段(= admin_rate)对帖子进行排序

function sort_searchresult_by_admin_rate($k) {
if (!is_home()) {
$k->query_vars['meta_key'] = 'admin_rate';
$k->query_vars['orderby'] = 'meta_value';
$k->query_vars['order'] = 'DESC';
}
}
add_action('pre_get_posts','sort_searchresult_by_admin_rate');

例如,我有4个帖子:

Title1  admin_rate=3    post_date=25 February 2014
Title2  admin_rate=4    post_date=26 February 2014
Title3  admin_rate=1    post_date=27 February 2014
Title4  admin_rate=1    post_date=28 February 2014

使用sort_searchresult_by_admin_rate($ k),我得到以下结果:

Title2  admin_rate=4    post_date=26 February 2014 (GOOD!)
Title1  admin_rate=3    post_date=25 February 2014 (GOOD!)
Title3  admin_rate=1    post_date=27 February 2014 (BAD!)
Title4  admin_rate=1    post_date=28 February 2014 (BAD!)

我想通过meta_value订阅帖子,然后按日期DESC命令:

Title2  admin_rate=4    post_date=26 February 2014 (GOOD!)
Title1  admin_rate=3    post_date=25 February 2014 (GOOD!)
Title4  admin_rate=1    post_date=28 February 2014 (GOOD!)
Title3  admin_rate=1    post_date=27 February 2014 (GOOD!)

有办法吗?

感谢。

1 个答案:

答案 0 :(得分:0)

您可以拥有多个orderby值。请尝试以下方法:

$k->query_vars['orderby'] = 'meta_value date';