Home › Forums › Add-ons › Repeater Field › Sort by repeater date picker field using if have rows › Reply To: Sort by repeater date picker field using if have rows
I would loop through all the information and assign it to an array.
The code below is UNTESTED, but it gives you an idea of how I would go about it.
<?php
//declare array
$events = array();
date_default_timezone_set('America/Los_Angeles');
$today = strtotime(date('Ymd'));
if (have_rows('events')): while (have_rows('events')) : the_row();
//get the expire date
$expire = strtotime(get_sub_field('expire_on'));
//compare it to today and assign it a value
if($expire < $today):
$events[]['expired'] = true;
else:
$events[]['expired'] = false;
endif;
//assign the other information to key/value in the array
$events[]['events_date'] = strtotime(the_sub_field('events_date'));
$events[]['events_title'] = the_sub_field('events_title');
$events[]['events_location'] = the_sub_field('events_location');
endwhile; endif;
//assign some variables for sorting
foreach ($events as $key => $row) {
$ev_expire[$key] = $row['expired'];
$ev_date[$key] = $row['events_date'];
$ev_title[$key] = $row['events_title'];
$ev_location[$key] = $row['events_location'];
}
//sorts by 'expired' (true first), then by date (descending), then by title (ascending)
array_multisort($ev_expire, SORT_DESC, $ev_date, SORT_DESC, $ev_title, SORT_ASC,$events);
//loop through the array and echo out the info if the event has expired
foreach ($events as $event){
if($event['expired'] == true) {
echo "<div class='events-row expired'>";
echo "<h2>".$event['events_title']."</h2>";
echo "<h3>".date('Ymd',$event['events_date'])."|".$event['events_location']."</h3>
</div>";
}
}
?>
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.