Support

Account

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

  • 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?