Support

Account

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

Solving

ACF Dynamically populate a select field’s choices, even when values are the same

  • I’m using fields from an options page to populate a select field on another page. Using 2 sub fields to create a value and a label for each choice.

    I got the code from ‘Example 2’ on this page https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    It was working fine, however I’ve just noticed a problem. When there are multiple options with the same value, it only shows the first one. This is a real problem as multiple options of mine have the same value. The label is always different though.

    Any ideas how I’d stop that from happening and making sure it registers each option as a seperate choice in the select field?

    function acf_load_color_field_choices( $field ) {
    
    // reset choices
    $field['choices'] = array();
    
    // if has rows
    if( have_rows('my_select_values', 'option') ) {
    
        // while has rows
        while( have_rows('my_select_values', 'option') ) {
    
            // instantiate row
            the_row();
    
            // vars
            $value = get_sub_field('value');
            $label = get_sub_field('label');
    
            // append to choices
            $field['choices'][ $value ] = $label;
    
        }
    
    }
    
    // return the field
    return $field;
    
    }
    
    add_filter('acf/load_field/name=color', 'acf_load_color_field_choices');

    Thanks

  • You can’t, all values must be unique. ACF stores the value/label pairs in an array like this:

    
    array(
      'value1' => 'label1',
      'value2' => 'label2'
    )
    

    the values are the array indexes and array indexes must be unique. This is the same for any field type that can have multiple values.

  • 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 🙂

  • You would need to loop through the repeater for each entry until you find the value that matches the label. Or you could do it all at once

    
    $value_lookup = array();
    while (have_rows('repeater')) {
      the_row();
      $value_lookup[get_sub_field('label')] = get_sub_field('value');
    }
    

    then you can use this array to look up the values

    
    $value = $value_lookup[$label];
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.