WordPress查询-按元字段值排序 [英] WordPress query - Order by meta-field value
本文介绍了WordPress查询-按元字段值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含三个元字段的帖子.
I have a post with three meta-fields.
add_post_meta($my_post, 'times', $times);
我想查询该类别并按其中一个的meta字段值对帖子进行排序.我现在使用的args是:
And i would like to query this category and sort the posts by the meta field value of one of them. The args i use right now are:
$args=array(
'post_type' => 'post',
'category_name' => 'players',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'times',
'meta_query' => array(
array(
'key' => 'times',
'value' => 0,
'compare' => '>=',
),
'posts_per_page'=> '8'
)
);
时间是元字段的名称.上面的代码未返回任何内容.
Where times is the name of the metafield.The above code isn't returning anything.
推荐答案
您有'posts_per_page'=>
. meta_query
参数中的'8'
You have 'posts_per_page'=> '8'
inside your meta_query
argument.
将您的代码更改为以下内容:
Change your code into the following:
$args=array(
'post_type' => 'post',
'category_name' => 'players',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'times',
'meta_query' => array(
array(
'key' => 'times',
'value' => 0,
'compare' => '>=',
)
),
'posts_per_page'=> '8'
);
这篇关于WordPress查询-按元字段值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文