2017-05-26 62 views
0

我想從下面的查詢中獲取數據。我想要兩個日期之間的結果通過第一個鍵和其他鍵的值如果等於1但是查詢返回值只有我今天的日期數據和sql add 的關係。請幫我弄清楚錯誤並加以糾正。WordPress的多元查詢

我的代碼是

$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 

感謝。

回答

0

在wp_query中比較日期時間,您需要將日期的元查詢項目轉換爲DATE Or DATETIME。

$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', 
    'type'=>'DATE' 
), 
array(
    'key' => 'agenda', 
    'value' => 1, 
    'compare' => '=' 
), 
),)); 

,如果這不是工作,你可以試試這個代碼

$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' => $start_date, 
      'compare' => '<=', 
      'type' => 'DATE' 
     ), 
array(
      'key' => '_EventEndDate', 
      'value' => $end_date, 
      'compare' => '>=', 
      'type' => 'DATE' 
     ) 
array(
    'key' => 'agenda', 
    'value' => 1, 
    'compare' => '=' 
), 
),)); 
+0

號兩者都不能正常工作。請看看我的SQL,這是主要問題。粗體文本返回錯誤的結果。只有今天的結果。 –

+0

您的代碼在我的服務器 http://downpublic.info/wpdemo/RS1991_ElseVillano/ –

+0

但爲我添加此**或(wp_postmeta.meta_value <='2017-05-26 05:30:23'AND tribe_event_end_date.meta_value > ='2017-05-26 05:30:23'))GROUP BY wp_posts.ID ORDER BY EventStartDate DESC,wp_postmeta.meta_value DESC LIMIT 0,10 ** –