2016-03-05 40 views
0

我在wordpress網站上有一個日期範圍搜索工具,用於幫助查找在特定時間範圍內發生的事件。我正在使用ACF pro的中繼器在事件帖子中列出日期。日期範圍工具正確地提取事件日期在日期範圍內發生的事件,但它也包括沒有在日期範圍內列出日期但第一個日期和最後日期位於日期任一側的事件範圍。使用meta_query確定日期範圍搜索

我需要找到一個解決方案,意味着只有日期範圍搜索中顯示的事件在範圍內具有嚴格的日期。

這裏是環/ meta_query我使用:

<?php // filter 
function my_posts_where($where) { 

$where = str_replace("meta_key = 'showings_%", "meta_key LIKE 'showings_%", $where); 

return $where; 
} 

add_filter('posts_where', 'my_posts_where'); 

    $_sda = $_GET['sda'] != '' ? $_GET['sda'] : ''; 
    $_smo = $_GET['smo'] != '' ? $_GET['smo'] : ''; 
    $_syr = $_GET['syr'] != '' ? $_GET['syr'] : ''; 

    $startd = $_syr.$_smo.$_sda.'0000'; 

    $_eda = $_GET['eda'] != '' ? $_GET['eda'] : ''; 
    $_emo = $_GET['emo'] != '' ? $_GET['emo'] : ''; 
    $_eyr = $_GET['eyr'] != '' ? $_GET['eyr'] : ''; 

    $endd = $_eyr.$_emo.$_eda.'2359'; 

    $meta_query = array(
         'posts_per_page' => -1, 
         'post_type'  => 'events', 
         'meta_key'  => 'showings_%_show_when', 
         'orderby' => 'meta_value', 
         'order' => 'ASC', 
         'meta_query' => array(
          'relation' => 'BETWEEN', 
          array(
           'key'  => 'showings_%_show_when', 
           'value'  => $startd, 
           'compare' => '>=', 
           'type'  => 'NUMERIC' 
          ), 
          array(
           'key'  => 'showings_%_show_when', 
           'value'  => $endd, 
           'compare' => '<=', 
           'type'  => 'NUMERIC' 
          ) 
         ) 
        ); 

    // query 
    $the_query = new WP_Query($meta_query); ?> 

我希望這是足夠的信息去。如果有幫助,這裏是live example

顯示不正確的事件是LegaC和MyMoves - 在此期間事件本身不會發生。

非常感謝,

回答

1

這已經解決了一點的重新起草的查詢。請看這裏:

$meta_query = array(
         'posts_per_page' => -1, 
         'post_type'  => 'events', 
         'orderby' => 'meta_value', 
         'order' => 'ASC', 
         'meta_query' => array(
          'relation' => 'AND', 
          array(
           'key' => 'showings_%_show_when', 
           'value' => array($startd, $endd), 
           'type' => 'NUMERICAL', 
           'compare' => 'BETWEEN' 
          ) 
         ) 
        );