Home › Forums › Add-ons › Repeater Field › How do I sort repeater field alphabetically? › Reply To: How do I sort repeater field alphabetically?
I spent yesterday working through this exact same question.
Here is what I found to work, applied to your case. Any PHP pros are welcome to optimise this. Note that I haven’t figured out the if statements, you should be able to get that working though.
<?php
// Get repeater value
$repeater = get_field('traditional_dining');
// Obtain list of columns
foreach ($repeater as $key => $row) {
$the_website[$key] = $row['website'];
$the_name[$key] = $row['name'];
$the_address[$key] = $row['address'];
$the_phone_number[$key] = $row['phone_number'];
$the_map_url[$key] = $row['map_url'];
}
// Sort the data by restaurant name column, ascending
array_multisort($the_name, SORT_ASC, $repeater);
// Display newly orded columns
// Unsure if this is the optimal way to do this...
foreach( $repeater as $row ) { ?>
<div class="biz-module">
<h2><a href="<?php echo $row['website']; ?>" title="Visit the website for <?php echo $row['website']; ?>"><?php echo $row['name']; ?></a></h2>
<h3><a href="<?php echo $row['map_url']; ?>" title="Find <?php echo $row['name']; ?> on the map"><?php echo $row['address']; ?></a></h3>
<h4><?php echo $row['phone_number']; ?></h4>
</div>
<?php } ?>
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.