I am trying to organize posts by year, based on what's been entered in the Advanced Custom Fields date picker (not the year published). I've been able to come very close to what I'm looking for using the year checker found on: http://alex.leonard.ie/2009/08/27/wordpress-grouping-posts-by-monthyear/
What I'm trying to accomplish is:
2015
- Past Exhibition
- Past Exhibition
- Etc.
2014
- Past Exhibition
- Past Exhibition
- Etc.
2013
- Past Exhibition
- Past Exhibition
- Etc.
The only problem is, while I am successfully able to print the year, only one exhibition under each year is listed.
<?php if (have_posts()) : ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$today = date('Ymd');
$past_args = array(
'paged' => $paged,
'posts_per_page' => -1,
'post_type' => 'exhibitions',
'meta_query' => array (
array (
'key' => 'end_date',
'value' => $today,
'compare' => '<',
'type' => 'DATE'
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
query_posts( $past_args );
?>
<!-- PAST EXHIBITIONS -->
<?php while (have_posts()) : the_post(); ?>
<?php
$date = get_field('end_date');
$past_year = DateTime::createFromFormat('Ymd', $date);
$year = $past_year->format('Y');
if ($year !== $today) { ?>
<article class="year-<?php echo $year; ?> child-page four columns">
<h2><?php echo $year; ?></h2>
<ul>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
</ul>
</article>
<?php }
$today = $year; ?>
<?php endwhile; ?>
<?php endif; ?>
What am I missing/doing wrong?