我正在为一个具有事件列表功能的站点编写一对查询。活动可以在一个日期或多个日期(多天)进行。所有事件在自定义字段中都有一个值event_date
. 多日活动也有end_date
价值
单日事件应在“即将到来的事件”列表(第一次查询)中,直到其结束event_date
, 多日活动应持续到活动结束end_date
. 一旦他们不再在即将到来的列表中,他们应该移到“过去的事件”列表(新查询,同一页)。
即将进行的查询工作正常。多日活动在其event_date
和end_date
值:
$now = date(\'Ymd\', strtotime("now"));
$future = date(\'Ymd\', strtotime("+24 months"));
$uc_args = array(
\'post_type\' => \'event\',
\'meta_query\' => array(
\'relation\' => \'OR\',
// check post has end_date and it is between $now and $future
array(
\'key\' => \'end_date\',
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\',
\'value\' => array($now, $future),
),
// if no end date check event_date is between $now and $future
array(
\'key\' => \'event_date\',
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\',
\'value\' => array($now, $future),
),
),
\'meta_key\' => \'event_date\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
\'nopaging\' => true,
);
过去的查询是以完全相同的方式构造的,但多日事件在其
event–date
已经过去了,即使他们有
end_date
值:
$past = date(\'Ymd\', strtotime("-999 months"));
$now = date(\'Ymd\', strtotime("now"));
$uc_args = array(
\'post_type\' => \'event\',
\'meta_query\' => array(
\'relation\' => \'OR\',
// check post has end_date and it is between $past and $now
array(
\'key\' => \'end_date\',
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\',
\'value\' => array($past, $now),
),
// if no end date check event_date is between $past and $now
array(
\'key\' => \'event_date\',
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\',
\'value\' => array($past, $now),
),
),
\'meta_key\' => \'event_date\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'nopaging\' => false,
\'posts_per_page\' => \'8\',
);
我可以看到OR关系如何包含所有满足任一条件的帖子,但在第一个查询中它没有这样做。据我所知,没有其他关系,但这就是我在这里的目标。
为什么这适用于即将举行的活动,但不适用于过去的活动?