我正在尝试从以下查询中获取数据。如果等于 1,我希望通过第一个键和其他键值在两个日期之间得到结果,但查询仅为我返回今天的日期数据并在 sql 中添加OR关系。请帮助我找出错误并得到纠正。
我的代码是
$curent_date = date('Y-m-d');
$start_date = date('Y-m-d 00:00:01', strtotime($curent_date."- 4 day"));
$end_date = date('Y-m-d 23:59:59', strtotime($curent_date."+ 4 day"));
$events_query = new WP_Query( array(
'post_type' => array('tribe_events'),
'post_status' => 'publish',
'meta_key' => '_EventStartDate',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_EventStartDate',
'value' => array( $start_date, $end_date ),
'compare' => 'BETWEEN'
),
array(
'key' => 'agenda',
'value' => 1,
'compare' => '='
),
),) );
print_r($events_query->request);
print_r($events_query->posts);
返回 SQL 是
SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.*, MIN(wp_postmeta.meta_value) as EventStartDate, MIN(tribe_event_end_date.meta_value) as EventEndDate FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) INNER JOIN wp_postmeta AS mt3 ON ( wp_posts.ID = mt3.post_id ) LEFT JOIN wp_postmeta as tribe_event_end_date ON ( wp_posts.ID = tribe_event_end_date.post_id AND tribe_event_end_date.meta_key = '_EventEndDate' ) WHERE 1=1 AND (
wp_postmeta.meta_key = '_EventStartDate'
AND
(
mt1.meta_key = '_EventStartDate'
AND
( mt2.meta_key = '_EventStartDate' AND mt2.meta_value BETWEEN '2017-05-22 00:00:01' AND '2017-05-30 23:59:59' )
AND
( mt3.meta_key = 'agenda' AND mt3.meta_value = '1' )
)
) AND wp_posts.post_type = 'tribe_events' AND ((wp_posts.post_status = 'publish')) AND **(wp_postmeta.meta_value >= '2017-05-26 02:58:14' OR (wp_postmeta.meta_value <= '2017-05-26 02:58:14' AND tribe_event_end_date.meta_value >= '2017-05-26 02:58:14' )) GROUP BY wp_posts.ID ORDER BY EventStartDate DESC, wp_postmeta.meta_value DESC LIMIT 0, 10
提前致谢。