我已经搜索了所有论坛和WP文档,但找不到WP元查询是否允许子元查询的答案。我的问题是:我有很多类型的促销(自定义帖子类型),有开始日期和结束日期(在db中存储为unixtime)。我想排除一种类型的促销(“墙”,效果很好),并且(使用或用于任何一种情况)排除开始日期和结束日期(如果存在)在过去的项目。这是我的代码:
$nowtime = time();
$the_query = new WP_Query( array(
\'post_type\' => \'promos\', \'orderby\' => \'menu_order\', \'order\' => \'ASC\', \'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'wpcf-type-of-promo\',
\'value\' => \'walls\',
\'compare\' => \'NOT LIKE\'
),
array(
\'relation\' => \'AND\',
array(
\'key\' => \'wpcf-date-and-time-start\',
\'value\' => $nowtime,
\'compare\' => \'<\',
\'type\' => \'NUMERIC\'
),
array(
\'key\' => \'wpcf-date-and-time-end\',
\'value\' => $nowtime,
\'compare\' => \'<\',
\'type\' => \'NUMERIC\'
))
) ));
对于上述代码,promo类型的“walls”被正确过滤掉,但子查询中的开始日期和结束日期被完全忽略。
SO网友:shanebp
我认为你不需要relation
参数默认为AND
.
$nowtime = time();
$the_query = new WP_Query( array(
\'post_type\' => \'promos\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'wpcf-type-of-promo\',
\'value\' => \'walls\',
\'compare\' => \'NOT LIKE\'
),
array(
\'key\' => \'wpcf-date-and-time-start\',
\'value\' => $nowtime,
\'compare\' => \'<\',
\'type\' => \'NUMERIC\'
),
array(
\'key\' => \'wpcf-date-and-time-end\',
\'value\' => $nowtime,
\'compare\' => \'<\',
\'type\' => \'NUMERIC\'
)
)
));