Ashish Detroja
Ashish Detroja

Reputation: 1082

meta_query between two date in wordpress not working

I tried meta_query but not working.

Need post data between particular date 2016-01-01 to 2017-01-01 but not working my code is :

$args = array( 
     'post_type' => 'post',
    'order' => 'ASC',
    'orderby' => 'title',
    'category' => array( 7, 11, 463, 409 ),
    'post_status' => array('publish'),  
    'posts_per_page' => -1,
     'meta_query' => array(
        array(
            'key' => 'post_date',
            'value' => array("2016-01-01", "2017-01-01"),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        )
    )
    );

Upvotes: 0

Views: 1987

Answers (2)

Ashish Detroja
Ashish Detroja

Reputation: 1082

I found the solution please check below code :

    $args = array( 
         'post_type' => 'post',
        'order' => 'ASC',
        'orderby' => 'title',
        'category' => array( 7, 11, 463, 409 ),
        'post_status' => array('publish'),  
        'posts_per_page' => -1,
        'date_query' => array(
            array(
                'column' => 'post_modified',
                'after'     => $last_fetch_date,
                'before'    => $current_time,
                'inclusive' => true,
            ),
        ),
   );

Upvotes: 1

Noman
Noman

Reputation: 4116

You need to make sure your meta contains only date format, Test the below code also dump the query using var_dump or echo the query which runs depends on your args Run the query in database to verify the data is setted in that format same as you want to get it by query. comment out the category line to make sure your post is returning or not, IF Return then check post the category is selected in the posts

$start = '2016-01-01';
$end   = '2017-01-01';
$args  = array(
    'post_type'      => 'post',
    'posts_per_page' => - 1,
    'order'          => 'ASC',
    'orderby'        => 'title',
    'category'       => array(7, 11, 463, 409),
    'meta_key'   => 'post_date',
    'meta_query' => array(
        array(
            'key'     => 'post_date',
            'value'   => array($start, $end),
            'compare' => 'BETWEEN',
            'type'    => 'DATE'
        )
    )
);
// Make the query
$Query = new WP_Query();
echo 'SQL: '.$Query->request.'<br>'; // your query against args

Upvotes: 1

Related Questions