Date range find in wordpress

97 views Asked by At

In wordpress I have only publish_date(not meta_key) and meta_key "end_date"

Example: event from 2015-06-22(publish_date) to 2015-08-24(meta_ket = "end_date")

now I want search all events

  1. (from 2015-07-05 to 2015-07-24)

    or

  2. (from 2015-08-05 to 2015-09-25)

    and must show event in example

This code not work:

$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',
        ),
    ),
    
)
);

relation between meta key and data_query not work, current idea is create 2 query but I dont want do this

1

There are 1 answers

1
vrajesh On

try this : here I added timestamp in startdate and enadate meta key

$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****************/
),
);