Support

Account

Home Forums Backend Issues (wp-admin) Opt group in select field works, but only returns value but no label

Solving

Opt group in select field works, but only returns value but no label

  • Hi

    I followed some of the suggestions in the forum about the option to add optGroups to the select field. It works well back-end wise, but when I want to return the selected choice on the front-end(value and label as well) I only get the value two times.

    Here is the working filter for the optgroups:

    function acf_load_region_field_choices( $field ) {
    
        $field['choices'] = array(
          'Group 1' => array(
            'purple' => 'Purple',
            'white' => 'White'
          ),
          'Group 2' => array(
            'black' => 'Black',
            'white' => 'White'
          ),
        );
    
        return $field;
        
    }
    
    add_filter('acf/load_field/name=region', 'acf_load_region_field_choices');

    The field’s return value is set to ‘Both(array)’. However if I use this code on the front-end:

      $regionvalue = get_field('region'); 
      
      echo '<pre>';
      var_dump($regionvalue);
      echo '</pre>';
    

    I only get the choice value with var_dump, two times:

    array(2) {
      ["value"]=>
      string(6) "purple"
      ["label"]=>
      string(6) "purple"
    }
    

    Any tips why is this happening? I guess the back-end return value option only looks for the first level of the array, the optgroup in this case. Thank you very much in advance

  • Hi @balint

    Please keep in mind that get_field() function won’t load the field object, so the acf/load_field hook won’t be working. In this case, kindly use the get_field_object() function instead. This page should give you more idea about it (under the Display value and label section): https://www.advancedcustomfields.com/resources/select/.

    I hope this helps 🙂

  • Hey James

    Thank you for helping me out. However I’m not sure I got everything right, we are talking about the selected choice’s valua/label pair and not the field’s label right?

    I tried to use the code in the example.

    If I use this for testing:

    $field = get_field_object('region');
      echo '<pre>';
      var_dump($field);
      echo '</pre>';
    

    I still get this:

    ["value"]=>
      array(2) {
        ["value"]=>
        string(6) "purple"
        ["label"]=>
        string(6) "purple"
      }

    So it seems to me it records the value twice in the database?

    edit:

    oh yeah, I forgot this, please keep in mind that my array got another level thanks to the opt group modification in the first post. So using the documentation’s example, my choices array looks like this for example:

    
    ["choices"]=>
      array(16) {
        ["UK - East"]=>
        array(6) {
          ["norfolk"]=>
          string(7) "Norfolk"
          ["suffolk"]=>
          string(7) "Suffolk"
          ["essex"]=>
          string(5) "Essex"
          ["hertfordshire"]=>
          string(13) "Hertfordshire"
          ["bedfordshire"]=>
          string(12) "Bedfordshire"
          ["cambridgeshire"]=>
          string(14) "Cambridgeshire"
        }

    So the “Display value and label” example won’t work here because it can’t find the actual label? So I guess this is a question of how to get the data here. Sorry I’m not a pro at PHP. Thank you in advance.

  • Hi @balint

    That’s weird. The get_field_object() function should always return the array of the field, not the value. Could you please make sure that the page is executing the right code? Also, could you please make sure there’s no caching plugin that makes the value unchanged?

    If you are sure the page is executing the right code, could you please share the code? If it’s too long, then you can share it by using GitHub Gist. Also, could you please share the JSON or XML export file of your field group so I can test it on my installation?

    Last thing, could you please make sure that you set the Return Format of your select field to “Value”?

    Thanks 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Opt group in select field works, but only returns value but no label’ is closed to new replies.