Home › Forums › Add-ons › Repeater Field › Repeater sub field display by group based on value › Reply To: Repeater sub field display by group based on value
I was trying to do the same thing a while ago. Couldn’t figure out how to do it natively in ACF so I just made a normal PHP array, sorted it, then looped through it.
Full credit to the following (my solution is basically these stuck together):
http://stackoverflow.com/questions/5498718/how-to-sort-associative-array-using-sub-field-of-contained-associative-arrays-in
https://www.advancedcustomfields.com/resources/repeater/
<?php
// check if the repeater field has rows of data
if( have_rows('person_repeater') ):
$personArray = get_field('person_repeater');
function callback( $a, $b ) {
if ( $a['person_group'] > $b['person_group'] ) {
return 1;
} else if ( $a['person_group'] < $b['person_group'] ) {
return -1;
}
return 0;
}
uasort( $personArray, 'callback' );
foreach ($personArray as $person) {
?>
Group name: <?php echo $person['person_group']; ?>, Person Name: <?php echo $person['person_name']; ?><br>
<?php
}
else :
// no rows found
endif;
?>
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.