Support

Account

Home Forums Front-end Issues Get Select Field Options Reply To: Get Select Field Options

  • 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).