I have a custom category template to group posts by a custom field 'event_date'. 2 things I cannot figure out, correctly: grouping by year and month, and having those in descending order, while the posts themselves are in ascending order. Currently this will create an 'event-year' div and 'event-month' for each post. I'm assuming I'll need some for/each statement for each 'year' and 'month.' But, I just don't have the experience to write it up correctly.
so the final markup would be:
<div event-year>
<div event-month>
<article>
<article>
</div>
<div event-month>
<article>
</div>
<div event-month>
</div>
<div event-year>
<div event-month>
<article>
<article>
</div>
</div>
This is what I have now:
<?
$the_query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_key' => 'event_date',
'orderby' => 'meta_value'
) );
$current_header = '';
$current_year = '';
$current_month = '';
$current_day = '';
# The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
# get the event date
$temp_date = get_post_meta( get_the_ID(), 'event_date', true );
Break up the date format
$dateTime = DateTime::createFromFormat("Ymd", $temp_date);
if ( is_object($dateTime) ) {
$month = $dateTime->format('F');
$year = $dateTime->format('Y');
$day = $dateTime->format('d');
//...
}
$display_month = substr($month, 0, 3);
if($year != $current_year) {
$current_year = $year;
echo "<div id='event-year' data-year-group='$year'>";
}
if($month != $current_month) {
$current_month = $month;
echo "<div class='event-month'><h2>$year - $month</h2>";
}
?>
<article> post stuff in here... </article>
<? endwhile; ?>