Support

Account

Home Forums Add-ons Repeater Field Repeater sorted by Date Reply To: Repeater sorted by Date

  • Quite right, thanks for having a look at the code for me.

    I modified it to use traditional syntax : rather than {} and added the endforeach but it was still limiting to current month only.

    I modified the loop to output month and one entry, and then kept the else statement the same. It not outputs month name, and all entries in chronological order. I’m not 100% sure this is the best way to achieve this, but it appears to work on the front end and doesn’t throw and php errors on the back end.
    Code:

    	    <?php
                 
                 $repeater = get_field('races');
                 foreach( $repeater as $key => $row ) :
                   
                     $column_id[ $key ] = $row['race_date'];
                   endforeach; 
                   array_multisort( $column_id, SORT_ASC, $repeater );
                   
    	       $currMonth = "start";
    	       foreach( $repeater as $row ) :
    	       
    		
    		    $date = DateTime::createFromFormat('Ymd', $row['race_date']);
    			
    			if ($currMonth != $date->format('F')) 
    				    :
    				    echo '<tr class="month"><td><h2>' . $date->format('F') . '</h2></td></tr><br>';
    				    $currMonth = $date->format('F');
    				    echo '<tr class="race">';
    				
    				       echo '<td class="cell">' . $date->format('m/d') . '</td>';
    				       echo '<td class="cell"><h2>' . $row['race_name'] . '</h2></td>';
    				       echo '<td class="cell">' . $row['race_location'] . '</td>';
    				       echo '<td class="cell">' . $row['race_gender'] . '</td>';
    			echo '</tr>';
    				    
    			else : 
    			
    			echo '<tr class="race">';
    				
    				       echo '<td class="cell">' . $date->format('m/d') . '</td>';
    				       echo '<td class="cell"><h2>' . $row['race_name'] . '</h2></td>';
    				       echo '<td class="cell">' . $row['race_location'] . '</td>';
    				       echo '<td class="cell">' . $row['race_gender'] . '</td>';
    			echo '</tr>';
    			
    			endif;
    			
    
    		endforeach; 
                   ?>
    

    Thanks for starting this thread, super helpful!