Support

Account

Home Forums General Issues Order Date Fields By Year, Then Month

Solving

Order Date Fields By Year, Then Month

  • Hi, I have an events page and want to list the events (current and past) in a year, then month format. In the following code my years show up but in a random order and the month shows the posts but the same one, with the same link.

    I’m looking to do something like this:
    2019
    Jan
    Event
    Event…

    Feb
    Event…

    2018
    March
    Event…
    Event…

    Dec
    Event…

    2017…

    Can anyone help point me in the right direction?

    $posts = get_posts(array(
                    'post_type' => 'events',
                    'meta_key'  => 'event_category',
                    'meta_value'=> 'Indoor Track and Field',
                    'order'     => 'ASC',
                    'orderby'   => 'meta_value_num',
                    'posts_per_page'    => -1,
                ));
    
                $group_posts = array();
    
                if( $posts ) {
    
                    foreach( $posts as $post ) {
    
                        $date = get_field('event_date', $post->ID, false);
    
                        $date = new DateTime($date);
    
                        $year = $date->format('Y');
                        $month = $date->format('F');
    
                        $group_posts[$year][$month][] = array($post, $date);
    
                    }
    
                } ?>
    
                <?php foreach ($group_posts as $yearKey => $years) {
                 
                    echo '<h3>Year: ' . $yearKey . '</h3><hr />';
                     
                    foreach ($years as $monthKey => $months) {
    
                        echo 'Month: ' . $monthKey . '<br />';
    
                        foreach ($months as $postKey => $posts) {
    
                            echo '<li class="title"><strong>';
                            if( get_field( 'date_range' )) { 
                                echo get_field( 'event_start_date' ) . ' ' . get_field( 'event_end_date' ) . ' : '; 
                              } else if( get_field( 'event_date' )) { 
                                echo get_field( 'event_date' ) . ' : ';
                              } 
    
                            echo the_title(); echo '</strong></li>';
                        }
    
                       
    
                    }
    
                }

    Thanks

  • UPDATE. I found a similar question in the support pages so now my code is:

    $currentdate = date("2000-01-01");
    
        /* Order Posts based on Date Picker value */
        $posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'events',
            'meta_key' => 'event_category', 
            'meta_value'=> 'Indoor Track and Field',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
                'meta_query'=> array(
                    array(
                        'key' => 'event_date',
                        'compare' => '>',
                        'value' => $currentdate,
                        'type' => 'DATE',
                ))
        ));
    
            $years = array();
    
            if( $posts ) {
    
                foreach( $posts as $post ) {
                    setup_postdata( $post );
                            
                        $date = date_create( get_field('event_date') );               
                        $year = date_format($date,'Y');
                            
                            if( !isset( $years[ $year ]) ) {
                                $years[ $year ] = array(
                                    'title' => $year,
                                    'posts' => array()
                                );
                            }
                            
                            $years[ $year ]['posts'][] = $post;
                            
                } wp_reset_postdata();
                
            }
    
            if( $years ) {
    
                foreach( $years as $year ) {
    
                    echo '<h2>' . $year['title'] . '</h2>';
                    echo '<ul>';
    
                    if( $year['posts'] ) {
    
                        foreach( $year['posts'] as $post ) {
                            setup_postdata( $post );
                            $date = date_create( get_field('event_date') );
                            
                                echo '<li><a href="';
                                the_permalink();
                                    echo '">';
                                    the_title();
                                    echo '</a><br>';
                                    echo date_format($date,'F d, Y');
                                    echo '</li>';
    
                        }
    
                    } echo '</ul>';
    
                } wp_reset_postdata();
    
            }

    The issue now is that the year is ordered past to present (2017, 2018, 2019) and the dates are not sorting correctly, meaning Jan, Feb, Mar, Apr…

    Can anyone help me order the year to start with 2019 and order the months by date?

    Thanks in advance.

  • Here’s My Update. I found this post in the support pages and have updated my code to the following:

    $currentdate = date("2000-01-01");
    
            /* Order Posts based on Date Picker value */
            $posts = get_posts(array(
                'posts_per_page' => -1,
                'post_type' => 'events',
                'meta_key' => 'event_category', 
                'meta_value'=> 'Indoor Track and Field',
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
                    'meta_query'=> array(
                        array(
                            'key' => 'event_date',
                            'compare' => '>',
                            'value' => $currentdate,
                            'type' => 'DATE',
                    ))
            ));
    
                $years = array();
    
                if( $posts ) {
    
                    foreach( $posts as $post ) {
                        setup_postdata( $post );
                                
                            $date = date_create( get_field('event_date') );               
                            $year = date_format($date,'Y');
                                
                                if( !isset( $years[ $year ]) ) {
                                    $years[ $year ] = array(
                                        'title' => $year,
                                        'posts' => array()
                                    );
                                }
                                
                                $years[ $year ]['posts'][] = $post;
                                
                    } wp_reset_postdata();
                    
                }
    
                if( $years ) {
    
                    foreach( $years as $year ) {
    
                    echo '<div class="panel panel-default">';
                    echo '<div class="panel-heading" role="tab" id="events-' . $year['title'] . '">
                            <h4 class="panel-title">
                                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse-events-' . $year['title'] . '" aria-expanded="true" aria-controls="collapse-events-' . $year['title'] . '">
                                    ' . $year['title'] . '
                                </a>
                            </h4>
                        </div>';
    
                        if( $year['posts'] ) {
    
                            foreach( $year['posts'] as $post ) {
                                setup_postdata( $post );
                                $date = date_create( get_field('event_date') );
                                
                                echo '<div id="collapse-events-' . $year['title'] . '" class="panel-collapse collapse" role="tabpanel" aria-labelledby="events-' . $year['title'] . '">
                                     <div class="panel-body">'; ?>
                                            
                                    <?php echo '<li><a href="';
                                    the_permalink();
                                        echo '">';
                                        the_title();
                                        echo '</a><br>';
                                        echo date_format($date,'F d, Y');
                                        echo '</li>'; ?>
                                            
                                <?php echo '</div></div>';
    
                            }
    
                        } echo '</div>';
    
                    } wp_reset_postdata();
    
                }

    When I strip the Bootstrap code the events show in a year, month format. The year is still older to newer (2017, 2018, 2019) and the dates are not ordered by ASC within the month. So we’re making progress.

    When I add the Bootstrap accordion I get the years, older to newer, and only one event in each year. Anyone shed some advice from my new code?

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

The topic ‘Order Date Fields By Year, Then Month’ is closed to new replies.