Support

Account

Home Forums General Issues Force Select Field Choices (Repeater) to Appear in Alphabetical Order

Helping

Force Select Field Choices (Repeater) to Appear in Alphabetical Order

  • Hi,

    I’ve searched everywhere and went through all the topics and replies including this one (https://support.advancedcustomfields.com/forums/topic/sort-asc-a-select-field-in-backend/) but can’t seem to find the solution I’m looking for.

    I have the following scenario and would like to force the select field/choices to appear in alphabetical order in both frontend (output) and the page editor (refresh order of existing rows after updating). The choices in the backend will be input in the correct order manually but I wouldn’t mind if that could be re-sorted automatically as well.

    – Field Group: Field Type “Repeater”
    – Sub Field 1: Field Type “Select” [countries dropdown]
    – Sub Field 2: Field Type “Number” [manual entry]

    Currently the output looks something like the following example, depending on the order the rows were created:

    United States 44 [entered 1st]
    Australia 18 [entered 2nd]
    China 27 [entered 3rd]

    However I require the table to display alphabetically (ASC) at all times, no matter the text value or what row was created first:

    Australia 18 [entered 2nd]
    China 27 [entered 3rd]
    United States 44 [entered 1st]

    Any help is much appreciated!

    Thanks.

  • The simplified PHP to get a better idea:

    <?php
    
    function acf_repeater_shortcode() {
    
    	ob_start();
    
    	if (have_rows('Table')) : ?>
    
    <table class="Table">
    
    	<tr>
    		<th class="Country">Chart</th>
    		<th class="Points">Position</th>
    	</tr>
    
    	<?php while (have_rows('Table')) : the_row(); ?>
    
    	<tr>
    		<td class="Country"><?php the_sub_field('Country'); ?></td>
    		<td class="Points"><?php the_sub_field('Points'); ?></td>
    	</tr>
    
    	<?php endwhile; ?>
    
    </table>
    
    <?php endif; ?>
    
    <?php return ob_get_clean();
    
    }
    
    add_shortcode('Table', 'acf_repeater_shortcode');
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.