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!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.