Home Forums Add-ons Repeater Field Output Repeater In Two Columns Sorted Alphabeticaly


Output Repeater In Two Columns Sorted Alphabeticaly

  • Hi Guys

    I have a requirement from a client to list a number of suppliers in two columns like this

    1 5
    2 6
    3 7

    After some googling, I was able to make this work with the following code

     <ul class="col-md-6">
                <?php $i = 0; $j = count( get_field('suppliers_table') );?> 
                <?php if( have_rows('suppliers_table') ): while ( have_rows('suppliers_table') ) : the_row();  ?>
                <li><a href="<?php the_sub_field('company_url'); ?>"> <?php the_sub_field('company'); ?></a></li>
                <?php if ( ( $i + 1 ) == ceil($j / 2) ) echo '</ul><ul class="col-md-6">'; ?>
                <?php $i++; endwhile; ?>
                <?php endif; ?>

    The trouble I am having is getting this to sort alphabetically on the front end. I spent a day googling this and nothing I tried worked. Now it may be my limited understanding of php here.

    I found a function in the ACF support pages to put in the function.php which sort, but it will not work. ( See below, I did include the correct field key)

    function my_acf_load_value( $value, $post_id, $field ) {
    	// vars
    	$order = array();
    	// bail early if no value
    	if( empty($value) ) {
    		return $value;
    	// populate order
    	foreach( $value as $i => $row ) {
    		$order[ $i ] = $row['field_57fcb8a09f91e'];
    	// multisort
    	array_multisort( $order, SORT_ASC, $value );
    	// return	
    	return $value;
    add_filter('acf/load_value/name=scores', 'my_acf_load_value', 10, 3);

    I am now kind of tearing my hair out over this. Is this even possible. Any help would be greatly appreciated. Thanks Guys

  • Hi @mrkeithy

    I believe you are on the right track. Kindly check this tutorial to learn how to sort a repeater:

    Please keep in mind that ‘field_57fcb8a09f91e’ should be the field key of the field you want to sort such as the company name.

    I hope this helps 🙂

  • Hi James

    Thanks for your reply, I did look at this page, It is the key, yet the function is not working. Is there anyway to build a sort into the two column loop I posted?

    Sorry i am kind of newbie with php.


  • Hi James

    Further to my last post, I finally made it work. I tried using the code on that page yesterday, but I must of been tired, because this morning and made it work. There may of been a small amount dancing around the room

    (see below)

          <ul class="col-md-6">          
                <?php $i = 0; $j = count( get_field('suppliers_table') );
                    $repeater = get_field('suppliers_table');
                    $order = array();
                    foreach( $repeater as $i => $row ) {
                        $order[ $i ] = $row['company'];
                    array_multisort( $order, SORT_ASC, $repeater );
                    if( $repeater ): ?>
                    <?php foreach( $repeater as $i => $row ): ?>
                        <li class="supplier_link">
                            <a href="<?php echo $row['company_url']; ?> "target="_blank"><?php echo $row['company']; ?></a>
                        <?php if ( ( $i + 1 ) == ceil($j / 2) ) echo '</ul><ul class="col-md-6">'; ?>
                    <?php $i++; endforeach; ?>
                <?php endif; ?>

    Only been at this fro two days.

    I still don’t understand why the function in functions.php is not working. Is there anyway to see if anything is conflicting with it?

  • Hi @mrkeithy

    The code looks correct for me. Just make sure that you changed the hook based on your field name like this:


    This page should give you more idea about it:

    Hope this helps 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Output Repeater In Two Columns Sorted Alphabeticaly’ is closed to new replies.