Home › Forums › General Issues › List events by date
I’m using the date picker field and I want to output all of the events sorted by date. I’ve written a query to do this, but it repeats the date for each event – I just want the date listed once with all of the events on that date under it. Can someone tell me the simplest way to do this? Here is my existing query:
<?php
//WordPress loop for custom post type
$schedule_query = new WP_Query('post_type=films&posts_per_page=70&meta_key=screening_date&order_by=screening_date');
while ($schedule_query->have_posts()) : $schedule_query->the_post(); ?>
<div class="screening">
<h2><strong><?php $date = DateTime::createFromFormat('Ymd', get_field('screening_date'));
echo $date->format('F d, Y'); ?>, </strong></h2>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="c5">Screening time: <?php the_field('screening_time'); ?>
</div>
<?php if( get_field('screening_details') ): ?>
<?php the_field('screening_details'); ?>
<?php endif; ?>
<div class="c7 end"><p class="venue">Venue: <?php the_field('venue'); ?><a href="http://www.brownpapertickets.com/producer/12556"><img src="<?php bloginfo('template_directory'); ?>/img/BPT_small_white.gif" width="100"></a>
</div>
<p class="film-link"><a href="<?php the_permalink(); ?>">View Film Details »</a>
</div>
<hr>
<?php endwhile; wp_reset_query(); ?>
You could check to see if the date has changed from the last event displayed, and if it has, then you display the date; otherwise you just display the event.. something along the lines of:
// initialize the last displayed date.. since this is before the loop, we'll just make it an empty string
$lastDate = '';
while ($schedule_query->have_posts()) : $schedule_query->the_post(); ?>
<?php // now that we're in the loop, start outputting events ?>
<div class="screening">
<h2><strong><?php $date = DateTime::createFromFormat('Ymd', get_field('screening_date'));
// now that we have the event's date, see if it matches the last date displayed.. if not, we'll update $lastDate with the event's date and display it
if ($date != $lastDate) {
$lastDate = $date;
echo $date->format('F d, Y'); ?>, </strong></h2>
}
...
endwhile;
?>
Thanks, we got this done yesterday by doing almost exactly that. 🙂
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 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.