NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Sep 10 2022
Estimated reading time : 1 minute, 18 seconds - 145 words
Share the post "WordPress Query Post by Date"
With this tutorial, we will create a query that will display Post Type posts from the last 7 days. We will store our arguments inside a variable, $semaine, and we loop it.
Le Query Post
For this example, our Post Type will be viavideos. Replace it with your own Post Type or replace by post for your articles.
The posts_per_page is set to -1 since we want to do a Query Post for 7 days since we don’t want it to spread across multiple pages. If you want to paginate it, simply specify the number of results you want per page; for example 5.
We will thus create a query with date_query which will fetch the data from the last 7 days. Notice that we specified the status (post_status) needs to be publish and we added orderby Date.
WordPress Query Post by Date
<?php $semaine = new WP_Query( array( 'post_type' => 'viavideos', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC', 'date_query' => array( array( 'after' => '1 week ago' ) ) )); while ( $semaine->have_posts() ) : $semaine->the_post(); ?> <!-- Votre contenu --> <?php wp_reset_query(); ?> <?php endwhile; ?>