NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Intermediate
Web Hosting Canada

Last modified : Sep 10 2022

Estimated reading time : 1 minute, 18 seconds - 145 words

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-&gt;have_posts() ) : $semaine-&gt;the_post(); ?>

<!-- Votre contenu --> 

<?php wp_reset_query(); ?>
<?php endwhile; ?>	

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>