Home Forums Add-ons Repeater Field How do I sort repeater field alphabetically?


How do I sort repeater field alphabetically?

  • I’ve been searching the website for how to display a list of repeater field entries alphabetically, but can’t find anything.

    I’m wanting to display a list of businesses, displaying the business name, address, phone number and order them alphabetically by the business name.

    I tried utilizing the example from this page, but was lost as to what code to put in this particular section (below) – Plus, this would only order by the ID number, not alphabetically based on the value of one of the fields.

    foreach( $repeater as $row )
    	// ... What do you put here?

    Here is the code I’m using right now – how would I sort this alphabetically by the ‘name’ sub-field?

    				// check if the repeater field has rows of data
    				if( have_rows('traditional_dining') ):
    					// loop through the rows of data
    					while ( have_rows('traditional_dining') ) : the_row();
    						<div class="biz-module">
    						<h2><?php if(get_sub_field('website')) {?><a href="<?php the_sub_field('website'); ?>" title="Visit the website for <?php the_sub_field('name'); ?>"><?php }?><?php the_sub_field('name'); ?><?php if(get_sub_field('website')) {?></a><?php }?></h2>
    						<h3><a href="<?php the_sub_field('map_url'); ?>" title="Find <?php the_sub_field('name'); ?> on the map"><?php the_sub_field('address'); ?></a></h3>
    						<h4><?php the_sub_field('phone_number'); ?></h4>
    					<?php endwhile;
    				else :
    					// no rows found
  • Just wondering if there will be a response from support here or not.

  • 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.

    // 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>
    <?php } ?>
  • this may be a quicker way –

    $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
    foreach ($fruits as $key => $val) {
        echo "$key = $val\n";
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘How do I sort repeater field alphabetically?’ is closed to new replies.