Get an Array of Values from an ACF Repeater Choice SubField
Get an array of values from an ACF repeater sub-field choice box to use further in your code.
So in the sap_colors sub-field above I want to capture the red,blue and green values into an array and have that array reflect any further additions or deletions. You can get this via get_sub_field_object() function and you have to run it in a have_rows loop…
<?php if( have_rows('repeater_field') ): ?> <?php while( have_rows('repeater_field') ): the_row(); ?> <?php // vars $select = get_sub_field_object('sap_colors'); $value = get_sub_field('sap_colors'); $sa_colors = $select['choices']; ?> <?php endwhile; ?> <?php endif; ?>
So above I am running the have_rows loop and using the get_sub_field_object on my repeater field I can get the values via $select[‘choices’] and declaring some variables for the array, now when I output my variable…
echo '<pre>'; print_r($sa_colors); echo '</pre>';
I have the array…
Array ( [red] => red [blue] => blue [green] => green )
Now if you add or delete more choices in the ACF field the array will be up to date.
*The linked ref uses has_sub_field() which is deprecated in favour of have_rows loop which is used in the above code snippet.