In this article, I’ll show you how to get posts published in the last 30 days using WP_Query!
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 30 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 30 days!
While I used it for the last 30 days, you can actually modify it to be however many days you want. Just change the “30” 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!
|$paged = get_query_var('paged') ? get_query_var('paged') : 1;|
|$args = array(|
|'post_type' => 'post',|
|'posts_per_page' => 12,|
|'date_query' => array(|
|'after' => '-30 days',|
|'column' => 'post_date',|
|'paged' => $paged|
|$loop = new WP_Query( $args );|
|while ( $loop->have_posts() ) : $loop->the_post();|
|// LOOP CONTENT HERE|
|<?php wp_pagenavi( array( 'query' => $loop ) ); ?>|
Leave a Reply