Home › Forums › General Issues › Separating future/past dates in two queries
I have a custom field for events for their start date (‘even_date_start’) and want to display on an archive page a list of all the events separated into upcoming events (sorted ASC) and past events (sorted DESC). I have tried to create two queries, but I have only had success in getting the upcoming section to work as desired. With the code shared below the past events go to the ‘else’ fallback which displays a message and no events. I’m sure I’m missing something simple, but I am stumped as to what it is. I’ve tried several things and am sharing my code in it’s current state. Any suggestions or keen eyes welcome!
`
<?php
/**
*/
get_header();
?>
<section id=”primary” class=”content-area”>
<main id=”main” class=”site-main”>
<div id=”post-wrap”>
<h2>Upcoming Events</h2>
<?php
// get today’s date
$date_now = date( ‘Y-m-d’ );
// get posts
$posts = get_posts(array(
‘post_type’ => ‘events’,
‘posts_per_page’ => -1,
‘meta_key’ => ‘event_date_start’,
‘orderby’ => ‘meta_value’,
‘order’ => ‘ASC’,
‘compare’ => ‘>=’,
‘value’ => $date_now,
‘type’ => ‘DATETIME’,
));
if( $posts ): ?>
<ul class=”events-list”>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<li>
<div class=”entry-content”>
<?php the_field( ‘event_date_start’ ); ?>
<?php if( get_field( ‘event_date_end’ ) ):
echo ” – “;
the_field( ‘event_date_end’ );
endif; ?>
<br/>
<span class=”event-title”><?php the_title(); ?></span>
<br />
<?php the_field( ‘event_location’ ) ?>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<? else: ?>
<p><?php _e( ‘No upcoming events are currently on the calendar.’ ); ?></p>
<?php endif; ?>
<hr />
<h2>Past Events</h2>
<?php
// get today’s date
$date_now = date( ‘Y-m-d’ );
// get posts
$past_args = array(
‘post_type’ => ‘events’,
‘posts_per_page’ => -1,
‘meta_key’ => ‘event_date_end’,
‘orderby’ => ‘meta_value’,
‘order’ => ‘DESC’,
‘compare’ => ‘<‘,
‘value’ => $date_now,
‘type’ => ‘DATETIME’,
);
$query_past = new WP_Query( $past_args );
if( $query_past->$posts ): ?>
<ul class=”events-list”>
<?php foreach( $query_past->$posts as $query_past->$post ):
setup_postdata( $query_past->$post )
?>
<li>
<div class=”entry-content”>
<?php the_field( ‘event_date_start’ ); ?>
<?php if( get_field( ‘event_date_end’ ) ):
echo ” – “;
the_field( ‘event_date_end’ );
endif; ?>
<br/>
<span class=”event-title”><?php the_title(); ?></span>
<br />
<?php the_field( ‘event_location’ ) ?>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<? else: ?>
<p><?php _e( ‘No past events are currently on the calendar.’ ); ?></p>
<?php endif; ?>
</div>
</main><!– #main –>
</section><!– #primary –>
<?php get_footer(); ?>
`
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
❓Ever wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
👉 https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.