This is a easy method to group custom posts by a date advanced custom field. I’m also going to sort by the date meta type.
<?php $args = array( 'post_type' => 'calendar-event', 'posts_per_page' => -1, 'meta_key' => 'ce_start_date', 'meta_type' => 'DATE', 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'ce_start_date', 'value' => date('Y-m-d'), 'compare' => '>=', ), ), ); $the_query = new WP_Query($args); $groupDate = null; if ($the_query->have_posts()) { while ($the_query->have_posts()) { $the_query->the_post(); $startDate = DateTime::createFromFormat('Y-m-d', get_field('ce_start_date')); if ($startDate->format('m') !== $groupDate) { if ($groupDate !== null) { echo "</section>"; } $groupDate = $startDate->format('m'); ?> <section date-group="<?php print $startDate->format('m'); ?>"> <h2><?php print $startDate->format('F') ." ". $startDate->format('Y'); ?></h2> <?php } ?> <!-- post data --> <?php } } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); ?> </section>