1

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; ?>
-1
$last_month = null;
$args = array(
    'post_type' => 'post',
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'post_status' => 'publish'
);

$the_query = new WP_Query($args);

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    $the_month = get_the_time( 'Ym' ); // e.g. 201611

    if ( $last_month !== $the_month ) {
        if ( $last_month !== null ) {
            // Close previously opened <div class="row" />
            echo '</div>';
        }

        echo '<br/><div class="row '.get_the_time("Y").'" >';
        echo 'MOnth: '.get_the_time('m');

    }

    $last_month = $the_month;

    ?>

    <div class="col-3">
        <?php the_title(); ?>
    </div>

    <?php

    if ( $the_query->current_post + 1 === $the_query->post_count ) {
        // Last item, always close the div
        echo '</div>';
    }

endwhile;
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Feb 20 '17 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.