I’m looking for a way for users to update the content of a WP query by choosing a year in a dropdown. I’d like to display the current year by default but let the users consult the archive of past years.
Is there a way to make it?
Thanks for your help !
Here’s the link to the page :
http://preprod.atelier-labotte.fr/municipalite/
Here’s my code :
<?php
$yearToLookFor = $_GET['year'];
$args = array(
'post_type' => 'comptes-rendus',
'post_status' => array( 'publish' ),
'posts_per_page' => '-1',
'date_query' => array('year' => $yearToLookFor ),
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
?>
<section class="wp-block-group light-color-section" id="compte-rendus">
<div class="wp-block-group__inner-container">
<div class="d-flex justify-content-between align-items-center">
<h2>Comptes rendus du conseil municipal</h2>
<select class="dropdown-year" name="archive-dropdown" onchange="">
<?php wp_get_archives( array( 'type' => 'yearly', 'format' => 'option', 'show_post_count' => 1, 'post_type' => 'comptes-rendus') ); ?>
</select>
</div>
<div class="d-grid columns-3">
<?php
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<div class="document-card">
<h4><?php the_title(); ?></h4>
<p class="tiny"><?php the_date('d/m/Y'); ?></p>
<?php
$file = get_field('download_document');
if( $file ):
$url = wp_get_attachment_url( $file ); ?>
<a href="<?php echo esc_html($url); ?>" class="btn" >Télécharger</a>
<?php endif; ?>
</div>
<?php
}
?>
</div>
</div>
</section>
<?php
}
wp_reset_postdata();
?>