Date query not inclusive despite parameter being true

I’ve set up a small test query to get some posts from October 1 to October 10. I published a post today ( October 10 ) and with the date query parameter of include set to true, I would have expected this post to show up in the query results. However, it does not.

If I alter the post’s publish date to October 1, it does appear in the results. Though if I change inclusive to false, it still appears. Basically, inclusive doesn’t appear to do anything.

If inclusive isn’t the answer, is there another way to ensure that I get all posts from October 1 to October 10 including October 1 and October 10?

$args = array( 
    'date_query' => array(
                array(
                    'after' => '2014-10-01',  
                    'before' => '2014-10-10',
                    'inclusive' => true  
                ),

            )
    );

$posts = get_posts($args);
if( $posts ) { 
    foreach ($posts as $p) {
        echo $p->post_title . ' on ' . $post->post_date . '<Br>';
    } 
} else {
    echo 'no posts';
}

Edited to add that I just dumped the date query SQL and the the where clause is

” AND ( ( post_date >= ‘2014-10-01 00:00:00’ AND post_date <=
‘2014-10-10 00:00:00’ ) )”

So it isn’t actually inclusive on the full day at all. Filtering the value to

” AND ( ( post_date >= ‘2014-10-01 00:00:00’ AND post_date <= ‘2014-10-10 23:59:59’ ) )”

actually includes posts made during the day of October 10.

1 Answer
1

If you only want to work with yyyy-mm-dd strings, for example '2014-10-10', you could do something like:

'before' => '2014-10-10 + 1 day - 1 second',

'before' => '2014-10-10 + 86399 seconds',

'before' => 'tomorrow 2014-10-10 - 1 second',

'before' => '2014-10-10 tomorrow - 1 second',

or

just append '23:59:59' to your yyyy-mm-dd string

to get

'before' => '2014-10-10 23:59:59'

Leave a Comment