Support

Account

Home Forums General Issues ACF Dynamically populate a select field’s choices, even when values are the same Reply To: ACF Dynamically populate a select field’s choices, even when values are the same

  • Interesting. My labels will always be unique though, so I could switch them over.

    NOPE – Just realised if I do that, the user on the backend is picking from a random list of numbers, it is the complete list though 🙂

    So instead I think the solution is to populate that select box with JUST the label..

    if ( have_rows( 'protein', 2564 ) ) {
            
            // while has rows
            while( have_rows('protein', 2564) ) {
                
                // instantiate row
                the_row();
                
                $type = get_sub_field('class', 2564);
    
                $value = get_sub_field('grams', 2564);
                $label = get_sub_field('food_name', 2564);
    
                // append to choices
                $field['choices'][ $label ] = $label;
                
            }
        }

    (theres definitely a more streamline way of doing that)

    …and then to use that label to find the matching ‘grams’ number back on the options page (ID=2564). The problem is ‘grams’ is not a subfield of the food name, but rather a sibling.

    I’m sure there’s a solution here, just need to give it some thought.

    Thanks for your help, sorry for the rambling 🙂