以下参数用于WP_Query
检查是否可以创建游戏预订。post的元输入字段是使用ACF DateTime Picker
并设置为Y-m-d H:i:s
$start_date = \'2019-08-31 12:15:00\';
$end_date = \'2019-08-31 12:20:00\';
$args = array(
\'post_type\' => \'rezervacija\',
\'posts_per_page\' => -1,
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'relation\' => \'OR\',
array(
\'key\' => \'start_date\',
\'value\' => array($start_date, $end_date),
\'compare\' => \'BETWEEN\',
\'type\' => \'DATETIME\'
),
array(
\'key\' => \'end_date\',
\'value\' => array($start_date, $end_date),
\'compare\' => \'BETWEEN\',
\'type\' => \'DATETIME\'
)
),
array(
\'key\' => \'location\', // Look also for specific playground, not important for this question
\'value\' => 3,
\'compare\' => \'=\'
)
)
);
$query = new WP_Query($args);
此代码通常可以正常工作,但并不完全符合预期。如果已经创建了开始日期设置为12:00的预订(
2019-08-31 12:00:00
) 结束时间14:00(
2019-08-31 14:00:00
) 然后,如果一个新用户选择时间12:15,结束时间13:30,在这种情况下,由于它分别查找这些值,因此查询只能找到它应该找到的值。因此,通常,当时间在以前的预订时间内时,此查询无法正确查找数据。
如何修改此查询以搜索$start\\u date和$end\\u date介于值start\\u date和end\\u date之间的帖子?
SO网友:Awais
您可能希望在两个日期之间进行查询:
$query = new WP_Query(array(
\'post_type\' => \'rezervacija\',
\'posts_per_page\' => -1,
\'post_status\' => \'publish\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'start_date\',
\'value\' => current_time(\'Ymd\'), //change this according to your need.
\'compare\' => \'>=\',
),
array(
\'key\' => \'end_date\',
\'value\' => current_time(\'Ymd\'), //change this according to your need.
\'compare\' => \'<=\',
),
),
));
if ($query->have_posts()) :
while($query->have_posts()) :
$query->the_post();
the_permalink();
the_title();
endwhile;
endif;