[英]WordPress meta_query and orderby custom field
I have a Custom Post Type called " Films ", with a Repeater field called " Showings ", which have a field " start_datetime " of type DateTime...我有一个名为“ Films ”的自定义帖子类型,带有一个名为“ Showings ”的Repeater字段,它有一个DateTime类型的字段“ start_datetime ”...
I want to perform a query to get all those Films with a Showing in the next 7 days, and I want to order them by Showing's start_datetime.我想执行查询以获取在接下来的 7 天内放映的所有电影,并且我想通过放映的 start_datetime 订购它们。
I have this query:我有这个查询:
$query = new \WP_Query( array(
'numberposts' => -1,
'post_type' => 'film',
'meta_key' => 'showings_%_start_datetime',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'showings_%_start_datetime',
'value' => array( time(), strtotime( '+7 days' ) ),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
)
));
This grabs all the films that have a showing in the next 7 days correctly, but then it orders the films by the start_datetime of the FIRST showing, and I need it to order them by the start_datetime of the FIRST FUTURE showing... Any help please?这会正确抓取在接下来的 7 天内放映的所有电影,但是它会在第一次放映的 start_datetime 之前订购电影,我需要它在FIRST FUTURE放映的 start_datetime 之前订购它们......任何帮助请?
Example:例子:
Film A have a showing today.电影A今天放映。
Film B have a showing tomorrow and another showing 1 year ago.电影 B 明天放映,一年前放映另一场。
My results will be ordered: Film B and then Film A (because actually Film B is the one with a showing starting eralier).我的结果将被排序:Film B 然后是 Film A(因为实际上 Film B 是具有放映起始时间的那个)。 But I need it to be ordered the other way around, because Film A is the film with the first future showing...
但我需要以相反的方式订购它,因为电影 A 是第一部未来放映的电影......
I discovered the problem by inspecting the actual SQL created by WP_Query
.我通过检查
WP_Query
创建的实际 SQL 发现了这个问题。 I realized that the generated SQL query has 2 INNER JOINs, one for the meta_key
and another for the actual meta_query
.我意识到生成的 SQL 查询有 2 个 INNER JOIN,一个用于
meta_key
,另一个用于实际的meta_query
。
Basically on one hand it is filtering those films that have a showings_%_start_datetime
in the next seven days, and on the other hand (independently) it is ordering the films by their showings_%_start_datetime
value.基本上,一方面它会过滤那些在接下来的 7 天内有
showings_%_start_datetime
电影,另一方面(独立地)它通过它们的showings_%_start_datetime
值对电影进行排序。
Just moving the meta_query
parameters to the base query works fine:只需将
meta_query
参数移动到基本查询即可正常工作:
query = new \WP_Query( array(
'numberposts' => -1,
'post_type' => 'film',
'meta_key' => 'showings_%_start_datetime',
'meta_value' => array( time(), strtotime( '+7 days' ) ),
'meta_type' => 'NUMERIC',
'meta_compare' => 'BETWEEN',
'orderby' => 'meta_value_num',
'order' => 'ASC',
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.