2

For a custom widget I want to retrieve the last posts published in the last hour.

All I can do actually is to display the posts of the day, with this query :

$queryArgs = array(
    'category_name' => 'myCategory',
    'posts_per_page'=> $number,
    'date_query' => array(
        array(
            'year' => date( 'Y' ),
            'month' => date( 'm' ),
            'day' => date( 'd' )
        ),
    ),
);
$query = new WP_Query($queryArgs);
3

You can try the following to query for last hour posts:

'date_query' => [
     [
         'after'     => '1 hour ago',  
         'inclusive' => true,
     ],
 ],

or

'date_query' => [
     [
         'after'     => 'last hour',  
         'inclusive' => true,
     ],
 ],

or

'date_query' => [
     [
         'after'     => '-1 hour',  
         'inclusive' => true,
     ],
 ],

Here we are playing with the strtotime() support in date_query.

| improve this answer | |
  • with this arguments wp_query doesn't return any post. – Elidrissi simo Jul 26 '17 at 19:08
  • what's the output of echo $query->request; ? – birgire Jul 26 '17 at 19:10
  • it works thank you very much . i used '-1 hour' – Elidrissi simo Jul 27 '17 at 6: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.