Home › Forums › Add-ons › Options Page › Dynamically populate a select subfield in a repeater field
I need to dynamically populate a subfield’s choices in a repeater field. I’ve read the tutorial and have successfully been able to create a function to load a field but don’t know how or if it’s possible to load a subfield.
I’m sorry @Tim Triplett, Can you be a little clearer in exactly what youre trying to do?
From what I gather, you are trying to do what is demonstrated in example 2 of the tutorial?
Cheers
I’m sorry too, for not being clearer.
I put a select sub-field in a repeater field. And I need to know how to write the function for it, in that it’s loading now into a sub-field.
This is how I did it before when select was a field.
//Dynamically populate a select field’s choices in ACF
function my_acf_load_field( $field )
{
// reset choices
$field[‘choices’] = array();
// load repeater from the options page
if(get_field(‘group_a_floor_plan’, ‘option’))
{
// loop through the repeater and use the sub fields “value” and “label”
while(has_sub_field(‘group_a_floor_plan’, ‘option’))
{
$value = get_sub_field(‘value’);
$label = get_sub_field(‘label’);
$field[‘choices’][ $value ] = $label;
}
}
// Important: return the field
return $field;
}
add_filter(‘acf/load_field/name=floor_plans_group_a’, ‘my_acf_load_field’);
Does anything change?
Hi again,
Can you please wrap your code in back ticks so that it is easier to read?
I am still not 100% on what you are trying to achieve because that code is looping through a sub field.
Sorry mate, sometimes it can be hard to get the full idea via a forum. 🙁
Thank you for your time and patience. I’ll try to make it more clear.
I have a repeater field in a field group. One of the subfields in that repeater group is a Select field type. I can’t get it to dynamically populate.
How should I change my function coding to get it to recognize a subfield Select? Or is this possible?
Hi @Tim Triplett
The issue here is most likely that your filter is not running. Have you added some debug code to your function to make sure it is running?
Perhaps also try using the sub field’s key
instead of it’s name
when adding the function to the filter like so:
add_filter(‘acf/load_field/key=field_123’, ‘my_acf_load_field’);
Please note, the filter should use the name or key of the SUB FIELD, not the repeater field.
Thanks
E
The topic ‘Dynamically populate a select subfield in a repeater field’ is closed to new replies.
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.