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

Intermediate

---- Last modified : Sep 08 2022

Estimated reading time : 1 minute, 30 seconds - 144 words

With this tutorial, we will show you a date based query post type stemming from a post meta. Here’s an example of a WP Query.

First step is to create a post meta date which fetches the date value. For example, we have created a post meta called informations_videos_release_date.

Now we will create a query that will display all the post type published between two specific dates. Meaning that for our exmaple, we will display the videos published between the two selected dates.

We will display videos between 1980 et 1989 by using a comparative meta query comparing to Post Meta Key values.

We could also create a form that would fetch the data between both dates and instead of using 1980 and 1989, we would recuperate the form data.

Create a Query Post Type Between Two Dates

<?php 
// Première valeur de date comparative 
$start = '1980'; 
// Deuxième valeur de date comparative 
$end = '1989'; 
// Nous commençons l'affichage 
$semaine = new WP_Query( 
array( 
'post_type'                  => 'viavideos', 
'post_status'                => 'publish',
'meta_key'                   => 'informations_vidos_date_de_sortie',
'showposts'                  => '200',
'orderby'                    => 'rand',
'paged'                      => $paged,
'order'                      => 'DESC', 
'meta_query'                 => array(
    array(
    // On appelle la meta key désirée et on compare entre les deux valeurs issus des deux variables
		'key'       => 'informations_vidos_date_de_sortie',
		'value'     => array($start, $end),
		'compare'   => 'BETWEEN',
	)
    )
));
while ( $semaine->have_posts() ) : $semaine->the_post();
?>

//// Votre loop ici ////

<?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>