Support

Account

Home Forums General Issues Order Post By Year, and Then 3 Months Period

Helping

Order Post By Year, and Then 3 Months Period

  • Hi, I have an news page and want to list the post by year and then 3 period months.
    I’m looking to do something like this:
    2020
    October – December
    – News
    – News

    July-September
    – News
    – News

    April-June
    – News
    – News

    January-March
    – News
    – News

    2019
    October – December
    – News
    – News

    July-September
    – News
    – News
    ….

    Can anyone help me?

    <?php              
    
                $currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
    
                /* Order Posts based on Date Picker value */
                $posts = get_posts(array(
                    'posts_per_page' => -1,
                    'post_type' => 'jmt_news', // name of custom post type
                    'meta_key' => 'date', // name of custom field
                    'orderby' => 'meta_value_num',
                    'order' => 'DESC',
                    'post_status'   =>'publish',
                    'meta_query'=> array(
                        array(
                          'key' => 'date',
                          'compare' => '<',
                          'value' => $currentdate,
                          'type' => 'DATE',
                    ))
                ));
    
                $years = array();
                $months = array();
                $groupMonths = [
    			    'October-December' => ['october', 'november' , 'december'],
                    'July-September' => ['july', 'august' , 'september'],
                    'April-June' => ['april', 'may' , 'june'],
                    'Januari-March' => ['january', 'february' , 'march']
    			];
    
                if( $posts )
                {
                    foreach( $posts as $post )
                    {
                        setup_postdata( $post );
                        
                        // get date
                        $date = date_create( get_field('date') );
                        
                        // get year
                        $year = date_format($date,'Y');
                        $month = date_format($date,'F');
                        
                        $group_posts[$year][$month]['posts'][] = $post;
                        
                    }
                    wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                }
    
                foreach ($group_posts as $yearKey => $years) {
                    echo  '<p>'.$yearKey.'</p>' ;
                                
                    foreach ( $groupMonths as $groupMonth => $monthArr) {
    
                        echo '<p>'.$groupMonth.'</p>';
    
                        foreach ($monthArr as $months) {
                        	foreach ($years as $months) {
                        		if( $months['posts'] ){
                        			foreach( $months['posts'] as $post ){
    		                            setup_postdata( $post );
    		                            
    		                            echo '<p>'; 
                                        echo the_title();
                                        echo '</p>';
    		                        }
                        		}
                        	}
                        }
                    }
                }
             ?>
  • If this is a date field (not a date time field) then your issue lies in the fact that ACF does not store “Date” values in the DB for the field. The values are in the format of “YYYYMMDD”. You’re queries must be on either “CHAR” or “NUMERIC” comparisons and any calculations based on the date must use this format.

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

The topic ‘Order Post By Year, and Then 3 Months Period’ is closed to new replies.