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

I am trying to loop through all the post to get the top most shared posts on social network. I want to use the date_query parameter in the WP_query to get the posts of last two days , last 5 days, last 7 days and last 9 days. How can i implement it through using the date_query in WP_Query
My WP_query is

<?php
 $args = array(
  'post_type' => 'post',
  'order'=>'DESC',
  'posts_per_page' => 1,
  'date_query' => array(
                     array(
        'after'     => '10 days ago',
        'inclusive' => true,
       ),
     ),
  'orderby'=>'meta_value',
  'meta_key'=>'esml_socialcount_TOTAL'
  );
 $the_query = new WP_Query($args);
 if($the_query->have_posts()) { 
 while ($the_query->have_posts()){$the_query->the_post();
?>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x