我需要获得与给定类别相关的帖子,并且:
事件开始日期(自定义字段)大于或等于今天的日期,或者事件结束日期(自定义字段)大于或等于今天(如果他们的事件开始日期小于今天的日期,则需要使用自定义开始字段对结果进行排序)。
我正在编写这段代码,但我找不到这个特定用例的相关文档。你能帮我找到一个好的在线解释,或者解释一下如何修复我的查询吗?
$since = date(\'Y-m-d\');
$args = array(
\'meta_key\' => \'event_date_start_id\',
\'category_name\' => \'private\',
\'order\' => \'ASC\',
\'orderby\' => \'meta_value\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
//event_date_start_id greater or equal to $since
\'key\' => \'bjab_event_date_start_id\',
\'value\' => $since,
\'type\' => \'numeric\',
\'compare\' => \'>=\'
),
array(
//event_date_end_id greater or equal to $since
//and event_date_start_id lower than $since
// and event_date_start_id != ""
\'relation\' => \'AND\',
array(
\'key\' => \'bjab_event_date_start_id\',
\'value\' => $since,
\'type\' => \'numeric\',
\'compare\' => \'<\'
),
array(
\'key\' => \'bjab_event_date_end_id\',
\'value\' => $since,
\'type\' => \'numeric\',
\'compare\' => \'>=\'
),
array(
\'key\' => \'bjab_event_date_end_id\',
\'value\' => \'\',
\'type\' => \'numeric\',
\'compare\' => \'!=\'
)
)
)
);
$query = new WP_Query( $args );
最合适的回答,由SO网友:cybmeta 整理而成
由于WordPress 4.1,可以构建嵌套meta, date and tax queries. 我还没有测试它,但下面的代码应该可以工作。此外,我建议替换PHPdate()
具有的函数current_time
从WordPress获取当前日期,同时考虑博客时区配置:
与您聊天后,您似乎只需要使用“DATE”作为type
论点
$since = current_time(\'Y-m-d\');
$args = array(
\'meta_key\' => \'bjab_event_date_start_id\',
\'category_name\' => \'private\',
\'order\' => \'ASC\',
\'orderby\' => \'meta_value\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
//event_date_start_id greater or equal to $since
\'key\' => \'bjab_event_date_start_id\',
\'value\' => $since,
\'type\' => \'DATE\',
\'compare\' => \'>=\'
),
array(
\'key\' => \'bjab_event_date_end_id\',
\'value\' => $since,
\'type\' => \'DATE\',
\'compare\' => \'>=\'
)
)
);
$query = new WP_Query( $args );