4

I'm getting some very strange results with the date_query parameter of get_posts. I have a post for which:

date('Y-m-d H:i', get_post_time('U', false, $postid))

returns:

2014-04-03 10:42

This post is returned fine from get_posts with these arguments:

$args = array(
    'post_type' => 'post',
    'cat' => 41,
    'fields' => 'ids'
);

But not with these:

$args = array(
    'post_type' => 'post',
    'date_query' => array(
        'after' => '1980-01-01',
        'before' => '2100-01-01',
    ),
    'cat' => 41,
    'fields' => 'ids',
);

Other posts seem to come back fine. What could be up? Does date_query query something different to get_post_time?

  • Odd. Do you get the right post if you query JUST on the date_query? Have you tried wp_query instead of get_posts (shouldn't really make a difference but worth checking)? – vancoder Mar 5 '15 at 1:18
5

Your syntax is incorrect for your date_query. It should be an array of an array, not just an array.

I also suspect that your problem might be related to PHP and not Wordpress.

Your before date is invalid. PHP only supports dates between 13 December 1901 and 19 January 2038, so your dates need to between those two dates. For reference, see date

You would also want to include the 'inclusive' parameter and set it to true for exact matches

Your query should be something like

$args = array(
'post_type' => 'post',
'date_query' => array(
    array(
        'after' => '1980-01-01',
        'before' => '2038-01-01',
        'inclusive' => true,
    ),
),
'cat' => 41,
'fields' => 'ids',
);
|improve this answer|||||

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.