Support

Account

Forum Replies Created

  • I received a reply through email and wanted to post the code in case there are others with the same issue. Here’s the code that I’m now using:

    
    $date2018 = '2018';
    $date_range_2018 = array( $date2018 . '-01-01', $date2018 . '-12-31' );
    
    $posts2018_args = array(
                'post_type' => 'events',
                'posts_per_page' => -1,
                'meta_key' => 'event_date',
                'orderby' => 'meta_value_num',
                'order' => 'ASC',
                    'meta_query' => array(
                        'relation' => 'AND',
                        
                        array(
                            'key' => 'event_category',
                            'value' => 'Indoor Track and Field',
                            'compare' => '=',
                        ),
    
                        array(
                            'key' => 'event_date',
                            'value' => $date_range_2018,
                            'compare' => 'BETWEEN',
                            'type' => 'DATE',
                        ),
    
                    ),
                );
    
            $posts2018 = get_posts( $posts2018_args );
    

    Thanks

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

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

  • Thanks John! I adjusted the code a bit and it works great. Here’s what worked for me in case anyone else has this issue.

    $args = array( 
    	'posts_per_page'   => -1,
    	'post_type'     => 'events',
    	'order' => 'ASC', 
    	'meta_query' => array(
    		array(
    			'key' => 'display_on_upcoming_events',
    			'value' => '1',
    			'compare' => '=='
    		),
    		 'date_clause' => array(
    			'key' => 'event_date',
    		'compare' => 'EXISTS'
    					    ),
    					  ),
    					  'orderby' => 'meta_value_num',
    					  'meta_key' => 'event_date', 
    					);
Viewing 4 posts - 1 through 4 (of 4 total)