2

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
  • 1
    For anyone getting here in 2022, it looks like the bug in question was fixed in WordPress's core right after OP made this post. 'inclusive' => true should now work the way that you would expect it to. Feb 11, 2022 at 13:24

1 Answer 1

4

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'
4
  • It turns out that you can write 'before' => '2014-10-10 23:59:59 directly as the date query parameter since that is a supported date string. If you'll add that, I will accept this answer. Oct 10, 2014 at 14:33
  • yes because the $before and $after strings are taken through the strtotime() function in the WP_Date_Query() class.
    – birgire
    Oct 10, 2014 at 16:10
  • The "inclusive" behavior still doesn't seem totally intuitive to me, but I do understand it now, so thanks for the help. Oct 10, 2014 at 18:08
  • 1
    yes I agree, it turns out that the inclusive parameter only determines if the date comparisons are using < and > (false) or <= and >= (true).
    – birgire
    Oct 10, 2014 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.