How to get the posts published in last two days using WP_Query?

Here are two ideas for your date_query part:

1) After 2 days ago:

If you need posts published after current time, 2 days ago:

'date_query' => array(
     array(
         'after'     => '2 days ago',  // or '-2 days'
         'inclusive' => true,
     ),
 ),

then the corresponding SQL part is:

 post_date >= '2014-09-09 17:57:15'

if the current date-time is 2014-09-11 17:57:15.

2) After midnight 2 days ago :

If you need posts published after midnight, 2 days ago:

'date_query' => array(
     array(
         'after'     => 'midnight 2 days ago',
         'inclusive' => true,
     ),
 ),

then the corresponding SQL part is:

 post_date >= '2014-09-09 00:00:00'

if the current date-time is 2014-09-11 17:57:15.

You can than easily modify this to other day periods.

Leave a Comment