Home › Forums › Add-ons › Repeater Field › Sort by repeater date picker field using if have rows
I have a repeater called events.
I use a date picker field called expire_on to hide the events if they have passed the expiration date.
I would like to sort the events by the expiration date. Can someone help me modify my below code to do that? Thanks!
<?php if (have_rows('events')): while (have_rows('events')) : the_row(); ?>
<?php
date_default_timezone_set('America/Los_Angeles');
$today = date('Ymd');
$expire = get_sub_field('expire_on');
?>
<div class="events-row <?php if ($expire < $today) { ?>expired<?php } ?>">
<h2><?php the_sub_field('events_title'); ?></h2>
<h3><?php the_sub_field('events_date'); ?> | <?php the_sub_field('events_location'); ?></h3>
</div>
<?php endwhile; endif; ?>
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>";
}
}
?>
Thank you. I wrote it as an array and it works. I guess the array is necessary in order to sort the data before displaying it.
You must be logged in to reply to this topic.
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!
Version 1.8.0 is out! 🎉
— Block Visibility (@BlockVisibility) April 13, 2021
The new version features an integration with @wp_acf, an overhaul of the Date & Time control, and a number of other usability enhancements. Checkout the video overview 👇https://t.co/NoKbiefp0Q
© 2021 Advanced Custom Fields. Subscribe
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.