I needed this functionality on a recent project using WooCommerce. The client wanted to create a page where all of the products added within the last 10 days would display dynamically. I am pretty familiar with WP_Query so turned there to come up with a solution. This is what I ended up using to pull all products that had been published in the last 10 days!
While I used it for the last 10 days, you can actually modify it to be however many days you want. Just change the “10” to whatever you need to pull.
You’ll also notice that I have added WP-PageNavi functionality into the snippet to provide support for paging since I used this in a WordPress page template.
At any rate, hope this helps!
'post', 'order'=>'DESC', 'posts_per_page' => 12, 'date_query' => array( array( 'after' => '-10 days', 'column' => 'post_date', ), ), 'paged' => $paged ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); // LOOP CONTENT HERE endwhile; ?> $loop ) ); ?>
Return posts made over a year ago but modified in the past month
$args = array( 'date_query' => array( array( 'column' => 'post_date_gmt', 'before' => '1 year ago', ), array( 'column' => 'post_modified_gmt', 'after' => '1 month ago', ), ), 'posts_per_page' => -1, ); $query = new WP_Query( $args );
Get posts published between specific dates in wordpress
I want to get all posts published between 1st January – 31st December
$args = array( 'date_query' => array( array( 'after' => 'January 1st, 2015', 'before' => 'December 31st, 2015', 'inclusive' => true, ), ), ); $query = new WP_Query( $args );
How to Query WordPress Posts or Comments by Date or Time
To retrieve the posts or comments by date and time, you need to use the date query. The date query provides several parameters which allows you to use a time and date as conditional arguments while fetching posts.
Fetching Posts with Date Query
To fetch posts conditionally, we need to use the WP_Query function with the specified arguments. To get posts bound by a date or time limit, we need to specify a conditional date query. For example, to get all posts, published in the last week, you would have to frame your date query as follows:
$last_week_posts = new WP_Query( array('date_query' => array( array ( 'after' => '1 week ago',) ) ));
To get posts after 1st January 2014, and before 1st March 2014 your date query would be:
'date_query' => array( array ( 'year' => 2014, 'day' => 1, array('month' => array(1, 6 ), 'compare' => 'BETWEEN', ), ));
Fetching Comments with the Date Query
You can also use the date query to get the comments according to a time duration. As an example, say you had to get the comments which were not older than 10 days. You can use the get_comments function to get the comments, and use the data_query as one of the arguments for the function.
Thus to get recent comments (approved comments, posted at the most 10 days ago) , you have to do the following:
//set the arguments $args = array( 'orderby' => 'date', 'status' => 'approve', 'order' => 'DESC', 'date_query' => array( array( 'after' => '10 days ago', ) ) ); // get the comments using the arguments $comments = get_comments($args);
Date queries are a very important and useful feature which allows you to retrieve posts and comments based on a date and time parameter.