Support

Account

Home Forums Front-end Issues Get Select Field Options

Solved

Get Select Field Options

  • Hi,
    I have a select field for a Custom Post Type. On the front end of my site I want to create a search form where users can search for topics. I would like the form to show a drop down select field.

    So the question is how can I get the select field options to populate the form?

    Thanks

  • get_field_object() http://www.advancedcustomfields.com/resources/get_field_object/

    You’re best bet with this would be to use the field key, not the field name.

    
    $field = get_field_object('field_abc123456789');
    $choices = $field['choices'];
    
  • So, conversely, how do you SET a field object, ie. a Choice?

    I am using update_field to set checkbox values and, despite “Allow Custom” and “Save Custom” being set, the new values are not being saved as Choices, which is against my expectation.

    Is there something like an equivalent set_field_object that is required to save the same value as a Choice at the same time?

  • if a checkbox field has allow custom and saved custom both set to yes then update_field() should be causing the value to be added to the choices, under normal conditions.

    You’ll need to supply more information on the field and how update_field() is being called.

  • @hube2

    Like this…

    update_field('sector', $json_string->category->sector, $org_id_prefixed);
    update_field('industry_group', $json_string->category->industryGroup, $org_id_prefixed);
    update_field('industry', $json_string->category->industryGroup, $org_id_prefixed);
    update_field('subindustry', $json_string->category->industryGroup, $org_id_prefixed);

    Where, FYI, the incoming values are…

       [category] => stdClass Object
            (
                [sector] => Information Technology
                [industryGroup] => Technology Hardware & Equipment
                [industry] => Technology Hardware, Storage & Peripherals
                [subIndustry] => Computer Hardware
                [sicCode] => 73
                [naicsCode] => 51
            )

    When I enter a new Checkbox value in the GUI, manually, it is properly saved as a Choice.

    But, when I use update_field, it does not happen. This means the values 1. persist as editable text boxes, not just check boxes and 2. don’t actually even show in columns my wp-admin edit list…

    [No screenshots]

    The weird thing, however, is that, when I enter an arbitrary new value in the GUI, not only does the specific value get saved as a Choice and becomes visible in a column, it also shakes all values previously saved with update_field in to acting properly…

    So, when I add a new value “Test Value” to the “sector” Checkbox, it fixes all “sector” values as Choices – and also those of “industry_group”, “industry” and “subindustry”.

    This is a happy side effect, but it doesn’t feel like this should be the correct behaviour, and it won’t change the fact that using programmatic update_field alone doesn’t fix values as Choices.

    (Don’t want to hijack the thread, but my attempt to create a new one was diverted to support).

  • This screenshot is for a different entry, but illustrates how some values saved only with update_field have not solidified as Choices but others which have since been saved-over manually appear to have done so…

    NB. I am doing all this on a taxonomy term.

  • Where was the other topic?

    Is $json_string a json string or is it an “object” as you posted.

    If it’s a json string you can’t access the values in it using $json_string->category->sector unless you decode it first.

    $object = json_decode($json_string);

  • I also just realized that you are using update_field($field_name, $value). If these fields to not already have values for these posts, in other words, you are adding the post and then immediately trying to update them where they have never had a value, then update field will not work using the field name and you must use the field key.

  • Is $json_string a json string or is it an “object” as you posted.

    If it’s a json string you can’t access the values in it using $json_string->category->sector unless you decode it first.

    $object = json_decode($json_string);

    I’m already decoding …

    $json_string = json_decode($result);

    and echo 'sector: '.$json_string->category->sector.'<br />'; confirms that the specific value is getting plucked out.

    Need to alter my use of language/name. String result to start, object once decoded.

  • I also just realized that you are using update_field($field_name, $value). If these fields to not already have values for these posts, in other words, you are adding the post and then immediately trying to update them where they have never had a value, then update field will not work using the field name and you must use the field key.

    For info, in my case, I am attempting to save field values for taxonomy terms, rather than posts. In current test phase, the term/s in question already exist.

    I think you’re saying – if the term already exists but if it has not yet had a value for the ACF field eg. “sector” already saved previously, I have to use update_field with the field key, not name…

    This gets me thinking, “rather than hard-code in the field key, can I programmatically obtain the field key from the field name?
    I see in the post Get field key by name, @almbay uses…

    $field = get_field_object('listing_type');
    $field_key = $field['key'];

    So maybe I could/should do…

    $field = get_field_object('sector');
    $field_key = $field['key'];
    update_field($field_key, $json_object->category->sector, $org_id_prefixed);

    But, I tried. And, in this situation, $field_key is assigned no value.

    I’ve checked the get_field_object docs. Maybe I should add $org_id_prefixed as a get_field_object parameter… ?

    $field = get_field_object('sector', $org_id_prefixed);
    $field_key = $field['key'];
    update_field($field_key, $json_object->category->sector, $org_id_prefixed);

    But, no, $field_key is still blank.
    In fact, before that, $field itself is also blank.

    So, why is $field = get_field_object('sector', $org_id_prefixed); failing to put anything in $field?

    FYI, $org_id_prefixed‘s value is like company_7456, corresponding to a taxonomy term ID formulation, and this value is present.

    (My previous topic attempt did not appear to save and the forum asked me to send a ticket instead).

  • Theory: $field is blank for the same reason as the starting issue – that, until a field value is actually saved, the object does not actually have a field value obtainable by its name (?). And it will actually be impossible for me to programmatically obtain a field key using a field name, until that field is actually saved with some values? Am I right/wrong?

    Assuming that’s right, okay, I’ll bow to hard-code the field keys instead…

               // Convert Json
               $json_object = json_decode($json_result_string);
    
               // SAVE TO ACF GROUP:
    
               // Categories (source stdClass object):
    
               // sector
              $field_key = 'field_5c1bb19b00004';
              update_field($field_key, $json_object->category->sector, $org_id_prefixed);
    
              // industryGroup
              $field_key = 'field_5c1bb1f100005';
              update_field($field_key, $json_object->category->industryGroup, $org_id_prefixed);
    
              // industry
              $field_key = 'field_5c1bb36d55bf7';
              update_field($field_key, $json_object->category->industry, $org_id_prefixed);
    
              // subIndustry
              $field_key = 'field_5c1bb474dfb1a';
              update_field($field_key, $json_object->category->subIndustry, $org_id_prefixed);
    
              // sicCode
              $field_key = 'field_5c1bb495dfb1b';
              update_field($field_key, $json_object->category->sicCode, $org_id_prefixed);
    
              // naicsCode
              $field_key = 'field_5c1bb4bddfb1c';
              update_field($field_key, $json_object->category->naicsCode, $org_id_prefixed);
    
              // Tags (source Array):
              $field_key = 'field_5c0a9d251831a';
              update_field($field_key, $json_object->tags, $org_id_prefixed);

    For a test, I have wiped the pre-fixed “Choices” out of all fields in the field group editor, and re-saved the group. Also, I have run this code on a taxonomy term which has not previously had any of the values set. The outcome is this…

    That is, the values are saving to the fields, but still not in the way intended re: cementing Choices as per my original problem. Only the values for the “Tags” Checkbox field have been set as Choices, the rest have not…

    One thing that’s noticeable (this may be coincidence, or not) is that the source for “Tags” is an array from the Json (“tags”), whilst the source for the rest of the values is a “std Class Object”. See the red/green rectangles in each of the screengrabs…

    Any correlation in this discrepancy, or am I already doing things correctly?

  • No, you are correct. Regardless of the time of field, it the field does not exist, especially a field like a taxonomy field that contains more than simple text, you cannot get the value of the field after using update_field() with the field name. ACF cannot create the field key reference and this is why the field appears blank even when it has content. You need to use the field key to do the updates. It is possible to get the field keys, you are already using the field name, that you have to code in some way, you just need to substitute the field keys.

    Generally when I need to do this I create an array that holds field name => field key pairs

    
    $fields_refs = array(
      'name of field 1' => 'field_123456',
      'name of field 2' => 'field_234567',
      // etc...
    )
    

    Then I can do something like

    
    update_field($field_refs['name of field 1'], $value, post_id);
    
  • Per my last message, using just the field key…

              // sector
              $field_key = 'field_5c1bb19b00004';
              update_field($field_key, $json_object->category->sector, $org_id_prefixed);

    … successfully saves the value – but still not in such a way as to solidify it as a Choice.

  • Still curious to solve this. Is there a URL at which I can see my support tickets? I was sure I posted (since the forum rejected it), just before the busy period, but can’t find anything and feel like I may be going crazy. Thanks.

  • Sorry, I don’t know what is causing your issue. I have tried updating a field using a field key that is supposed to add to the choices when a new value is added and it is working for me, so I cannot recreate this issue. The only thing I can think of is that there’s something else interfering with the update.

    I don’t know anything about the other ticket system, or very little. As far as I know it just sends an email, but I could be wrong.

  • Okay, thanks John.
    Back to support…

  • For the historical record, the solution (per tech support) is…

    Via update_field(), Checkbox needs to take an array, not a string item.
    So my items had to be passed inside array() (convert them to array for supply to update_field().

    Apart from one of my lines which was already an array.

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

You must be logged in to reply to this topic.