Support

Account

Home Forums Backend Issues (wp-admin) Update field taxonomy

Solving

Update field taxonomy

  • Hello everyone,

    I would like to update a field taxonomy with mutiple value? if the term does not exist the term is created.

    I have this code :

    $term_id = term_exists( ‘Pianiste’, ‘Functions’, $parent ); // The term matched the ID is 178

    wp_set_post_terms( $post_id, $term_id->term_id, ‘Functions’ );

    But when i use the function wp_set_post_terms a new term is created with name 178 and the field doesn’t selected anything.

    Taxonomy field

    Can you help me ?

    Thank you

  • hi @signelazer

    If you’re setting it on a custom post type, you’ll need to use this function wp_set_object_terms instead. https://wordpress.stackexchange.com/questions/114576/wp-set-post-terms-not-work

    Also, term_exists sends back the ID, so you don’t need to do $term_id->term_id, just $term_id would work as found here: https://codex.wordpress.org/Function_Reference/term_exists

    I found this by searching “wp_set_post_terms not working” and it was in the top 3 links (at least for my results). When trying to debug a situation, try searching for what’s wrong and you’ll find that usually a lot of other people have struggled with the same issue.

    Here’s some a resource (provided by ACF) for debugging: https://www.advancedcustomfields.com/resources/debug/

    Let us know if this helps or if the problem persists!

    Phil

  • Hi @nathanaelphilip,

    Thank for reply.

    I know this function but it’s just create a new term if the term not exist.

    Now i would like to make the selected value into the acf field taxonomy.

    I see it’s a Select2.

    Thanks in advance.

  • Hello,

    Any other solutions i block for that.

    Thank you.

  • try

    
    wp_set_post_terms($post_id, array($term_id->term_id), 'Functions');
    

    or

    
    wp_set_object_terms($post_id, array($term_id->term_id), 'Functions');
    
  • Hi John,

    Thank you for the reply,

    I think i’m in good way,

    In the database the field is good

    functions database

    But the select field is not render the value.

    render field

    Thank you in advance.

  • Try using the update_field ACF API function. In my experience, the backend ACF UI wasn’t selecting the attached terms because they weren’t associated correctly in the database.

    update_field documentation:
    https://www.advancedcustomfields.com/resources/update_field/#usage

    Here is an example that uses a Gravity Forms hook. After a user submits the form, a new post is created. Once it’s created and the terms are attached to the post, I take those terms (array) and use the update_field ACF function to attach them. Now, my taxonomy checkboxes on the backend are correctly selected.

    
    add_action( 'gform_after_submission_1', 'update_acf_terms_for_rentals', 10, 2 );
    function update_acf_terms_for_rentals( $entry, $form ) {
    
        //get the post
        $post = get_post( $entry['post_id'] );
    
        // get the term IDs array (IDs only)
        $term_list = wp_get_post_terms($post->ID, 'space', array("fields" => "ids"));
    
        // Insert the $term_list into the database via ACF
        $field_key = 'field_5b50eb94713f1';
        update_field($field_key, $term_list, $post->ID);
    }
    
    
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Update field taxonomy’ is closed to new replies.