Home › Forums › ACF PRO › Sorting repeater fields by date › Reply To: Sorting repeater fields by date
Thank you for your help, I tried to use your code and it doesn’t seem to work quite yet. Also I made some modifications :
<?php
// get the entire repeater in a multidimensional array
$repeater = get_field('agenda_test');
// set starting month and count
// we need a counter to know if we're on the first row
$previous_month = '';
$count = 0;
foreach ($repeater as $row) {
echo "</br>";
var_dump($row);
// make sure that your date field is returning a valid date format
// or this will not work
$this_month = date('F', strtotime($row['date']));
if ($this_month != $previous_month) {
// month has changed
if ($count > 0) {
// close the previously opened container
// the container is opened below
?>
</div><!-- .month -->
<?php
} // end in count > 0 (i.e. not first row)
// open a new container and display the month
?>
<div class="month">
<div class="the_month"><?php echo $this_month; ?></div>
<?php
// update previous month value for next loop
$previous_month = $this_month;
} // end if new month
// output additional sub fields here
// increment count
$count++;
} // end foreach row
?>
And this what I get in return:
array(2) { [“date”]=> string(10) “02/02/2019” [“texte_test”]=> string(42) “Premier texte de ce qu’il se passe ce mois” }
February
array(2) { [“date”]=> string(10) “15/02/2019” [“texte_test”]=> string(28) “Un autre texte pour février” }
January
array(2) { [“date”]=> string(10) “05/04/2019” [“texte_test”]=> string(53) “Un texte pour le mois d’acril, mais écrit avant mars” }
May
array(2) { [“date”]=> string(10) “10/04/2019” [“texte_test”]=> string(29) “Ce texte est pour le 10 avril” }
October
array(2) { [“date”]=> string(10) “02/03/2019” [“texte_test”]=> string(27) “Le jour de mon anniversaire” }
February
I guess I have to format the date in some way, though I don’t know how. Also, I want the months in french if possible….
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.