如何在Wordpress 3.1中的meta_query中使用orderby?
2 个回答
- 投票数
-
- 2011-03-02
新的
的旧方法meta_query
数组选择查询返回的帖子.因此,是的,您在该meta_query
中指明了"键",但仍可以使用'orderby' => 'meta_value', 'meta_key' => '_events_meta',
,因为这些行指示了如何对结果查询进行排序.所以是的,您可能会两次指定相同的meta_key.
the new
meta_query
array selects which posts the query returns. So yes, you are indicating the 'key' within thatmeta_query
, but you can still use the old method of'orderby' => 'meta_value', 'meta_key' => '_events_meta',
in addition to the meta_query, as these lines indicate how to sort the resulting query. So yes, you might indicate the same meta_key twice.
-
这个答案并不完全正确.如果您在现有meta_query之外添加orderby和meta_key,则结果的确会按提供的键进行排序-但即使值不符合条件,它也会包括设置了该meta键的所有帖子在meta_query中(至少,这就是我在测试中的工作方式).更好的方法可能是将数组作为orderby参数传递,如下所述:http://core.trac.wordpress.org/ticket/17065#comment:14This answer is not completely right. If you add an orderby and a meta_key outside of an existing meta_query, the result will indeed sort by the provided key--but it will also then include any post where that meta key is set, even if the value doesn't meet the criteria in the meta_query (at least, that's how it worked for me in testing). A better way to do this might be to pass an array as the orderby param, as described here: http://core.trac.wordpress.org/ticket/17065#comment:14
- 9
- 2012-04-03
- MathSmath
-
- 2011-03-02
我对名为
events
的自定义帖子使用以下代码,以使所有帖子都处于循环中.$evtLoop = new WP_Query(array('post_type' => 'events', 'posts_per_page' => 10, 'orderby' => 'meta_value', 'meta_key' => '_events_meta', 'order'=>'DESC'));
我认为您使用代码的方式大致相同.我认为您缺少要排序的元字段名称的
,可能会有所帮助meta_key
.如果您添加'meta_key' => 'webinar_startDate',
到外部数组?
I'm using the following code for my custom posts called
events
, to get all posts in a Loop.$evtLoop = new WP_Query(array('post_type' => 'events', 'posts_per_page' => 10, 'orderby' => 'meta_value', 'meta_key' => '_events_meta', 'order'=>'DESC'));
I think you are using your code approximatly the same way. I think you are missing the
meta_key
with the name of the meta-field to sort. Perhaps it helps if you add'meta_key' => 'webinar_startDate',
to the outer array?
-
我现在明白了.我对Wordpress文档感到困惑,说"meta_key"和"meta_value"已贬值.我以为那意味着我不能使用它们,但是我想那是误导.谢谢!I see now. I was confused by the Wordpress documentation that said 'meta_key' and 'meta_value' were depreciated. I assumed that meant I couldn't use them, but I guess that was misleading. Thanks!
- 0
- 2011-03-02
- Jeff K.
用meta_query过滤后,是否可以通过我选择的元数据对自定义帖子列表进行排序?
例如,我有一个名为webinars的自定义帖子类型.我正在尝试列出所有即将举行的网络研讨会,并通过名为webinar_startDate的自定义元字段对它们进行排序.
使用以下查询,我成功地返回了网络研讨会,但不包括旧的网络研讨会.但是,它们仍然按发布顺序显示,而不是通过webinar_startDate出现.
我怀疑由于从3.0到3.1的变化,orderby=>meta_value的使用可能会有所不同,但是我无法在WordPress文档中找到答案来解释这一点.
有人可以帮忙吗?预先感谢.