日期范围在wordpress中找到

时间:2015-06-22 13:03:36

标签: php mysql wordpress date-range

在wordpress中我只有 publish_date (不是meta_key)和 meta_key end_date

示例:从2015-06-22(publish_date)到2015-08-24的事件(meta_ket =“end_date”)

现在我想搜索所有活动

  1. (2015-07-05至2015-07-24)

  2. (2015-08-05至2015-09-25)

    并且必须在示例中显示事件

  3. 此代码不起作用:

    $opt = array(
    'post_type'     => 'event_info',
    'posts_per_page'    =>  '40',
    'paged' => $page_num,
    'orderby' => 'post_date ID',
    'order' => 'DESC',
    
    'relation' => 'OR',
    
        'meta_query' => array(
            'key' => 'end_date',
            'value' => array('$date_start','$date_end'),
            'compare' => 'BETWEEN',
            'type' => 'numeric'),
    
    
        'date_query' => array(
            array(
                ......
                'compare'   => 'BETWEEN',
            ),
        ),
    
    )
    );
    

    元键和data_query之间的关系不起作用, 当前的想法是创建2查询,但我不想这样做

1 个答案:

答案 0 :(得分:-1)

试试这个:这里我在startdate中添加了时间戳并添加了元键

$chkdt = current_time('timestamp');
$args = array('post_type'=>'event_info', 'post_status' => 'publish','posts_per_page'=>-1,'orderby' => 'ID','order' => 'DESC', 'meta_query' =>
array(
'relation' => 'AND',    
/******filter start date and End date****************/
array(
'key' => 'end_date',
'value' => $chkdt,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'start_date',
'value' => $chkdt,
'compare' => 'NUMERIC',
),
/******END filter start date and End date****************/
),
);