Support

Account

Home Forums General Issues List events by date

Solved

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 &raquo;</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. 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘List events by date’ is closed to new replies.