Home Forums General Issues How to use update_field($field_taxonomy, $term_taxonomy, $post_id)?


How to use update_field($field_taxonomy, $term_taxonomy, $post_id)?

  • I use “Custom Post Type UI 1.3.3” & “ACF5”.

    I have a custom taxonomy: “request_status” (slugs: created, canceled, accepted, rejected, paid) and set it in ACF:

    How to use in PHP code update_field(‘_status’, ‘created’, $post_id)?
    That don’t work. It’s the post in wp-admin:

    My question about all complex object like Taxonomy, User, Relationship etc. What must I put to $value for working function?

    function acfSavePost( $post_id ) {
        $current_user = wp_get_current_user();
        if(wpa_post_exists($post_id) && get_post_type($post_id) == 'request') {
            if ( !is_admin() && is_user_logged_in() ) {
                update_field('_user', $current_user->ID, $post_id); // it works!
                update_field('_status', 'created', $post_id); // it don't work!
                update_field('_status', 5, $post_id); // it don't work!
    add_action('acf/save_post', 'acfSavePost', 20);

    I tried to google, but Google became all purple, but the answer I did not find… 🙂

  • So… The Support replied: “…you will need to pass the $term object to the $value parameter of the function”. Ok. Lat’s do it now!

    Inside “acf/save_post”:

    $current_user = wp_get_current_user();
    update_field('_user', $current_user, $post_id); // user_obj, but don't work, why is it?
    update_field('_user', $current_user->ID, $post_id); // just ID, but works fine
    $status_term_obj = get_term_by('slug', 'created', 'request_status'); // it's term_obj
    update_field('_status', $status_term_obj, $post_id); // don't work!

    In Debuger update_field() looks:

    The Tax_Term_Obj there is, but the Field_Obj is “null”. Why?

    Here is this field: and that field_group is added to post_type=”request”.

    Maybe it’s important: fields “_user” and “_status” in fields_group “Request Admin Part” and they don’t show in acf_form() and set in code “acf/save_post”.

  • Time is going… So, at first, this is for User Object.

    If you will use get_field() you will get OBJECT, but if you will set update_field() you must put ID or ARRAY_OF_ID (for multiselect field). It isn’t clear for using and I didn’t see an ACF tutorial for it.

    Will be great have a fine tutorial in Documentation “How to use update_field() for complex objects like User, Taxonomy, etc”. Because in wp-admin you just click mouse & select, but in code you must use correct format $value.

    At second, the question about “update_field() with field_taxonomy” is open.

  • It depends on the type of field your showing for a taxonomy field. If you are using a checkbox or multiselect then it stores and array of term IDs, if you have it set for radio of single select then it stores a single term ID.

    Here’s the way to tell what will be stored for most fields.

    If the field allows only a single value then it will be a single value.

    If the field allows multiple values then ACF will store and array.

    If the field is some type of relationship or you are choosing another object then ACF stores the ID of that object. This includes posts, pages, attachments (media), and terms.

    As an example, a gallery field allows you to select multiple images. ACF stores and array of attachment IDs.

    There are some fields that do not follow this logic, and I don’t recall exactly what they store. These are things like the Page Link, omebed and google map fields. I don’t use them much so I’ve never had a reason to look into them.

    Hope this help you.

  • Yes, it’s the full answer. Thanks.

    After some experiment, I understand where is the true problem. $value is correct, but something wrong with updating process:

  • You might be running into a known… it’s not an issue, but…

    If the post/field does not exist in the DB when you try to use update_field() using the field name it will not update. ACF doesn’t know what to do with the value. Try using the field key when updating the field.

  • “In the name of The Old Gods and the New” (с) 🙂
    Yes! It’s working! Thanks, John.

    I must use keys, like “field_5729dc2c38f13”, instead names of fields.

    The solution for hidden in acf_form() fields (By “hidden” I mean that I don’t show it the field to user in front-end):

    update_field('_status', $status_term_obj->term_id, $post_id); // don't work
    update_field('field_5729dc2c38f13', $status_term_obj->term_id, $post_id); // work!

    I hope that will help someone.

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

The topic ‘How to use update_field($field_taxonomy, $term_taxonomy, $post_id)?’ is closed to new replies.