Support

Account

Home Forums Backend Issues (wp-admin) update_field for taxonomy field type

Solved

update_field for taxonomy field type

  • Hi,
    I try to update an ACF field (taxonomy type) for a post with update_field but it does not work. This updates the wp_postmeta table but not the relationship between the post and taxonomy (wp_term_relationships).
    How to solve this problem ?
    Thank you
    Fred

    • James

    • December 22, 2016 at 1:33 pm

    Hi @fred-malinge

    The update_field() function won’t update the post taxonomy. To do that, you need to set the taxonomy by using the wp_set_post_terms() function. This page should give you more idea about it: https://codex.wordpress.org/Function_Reference/wp_set_post_terms.

    I hope this helps 🙂

  • Hi,
    thanks
    i use wp_set_object_terms for it s a custom posttype and custom tax, but it doesnt work. May be i cant call this function in the acf/save_post action ?
    update_field( ‘field_56618811a093f’, 18, $postID ); = OK
    $term_taxonomy_ids = wp_set_object_terms( $postID, 18, ‘my_taxonomy’); = doesnt work

    • James

    • December 23, 2016 at 12:53 pm

    Hi @fred-malinge

    Could you please share the complete code?

    Also, could you please try it by passing the term name instead? So it should be like this:

    $term_taxonomy_ids = wp_set_object_terms( $postID, 'term-name', 'my_taxonomy');

    Thanks 🙂

  • Hi,
    I tried several ways but none worked. With the slug, the id, … yet updat_field lines work

    here is my code, see // WP SET TERM OBJECT DOESNT WORK (line 57)

    Thanks
    Fred

    • James

    • January 16, 2017 at 11:46 am

    Hi @fred-malinge

    I’m afraid you can’t attach PHP file to the reply. Kindly zip it first before attaching it or use https://gist.github.com/.

    Thanks 🙂

  • Hi,
    here is a zip
    Thanks
    Fred

    • James

    • January 19, 2017 at 1:07 pm

    Hi @fred-malinge

    I’ve just tested wp_set_object_terms() function with the acf/save_post hook, and it’s working correctly. But please keep in mind that I modified the code so it’s working on my installation.

    Could you please make sure that the variables have the correct values? Could you please debug it like this:

    echo "\noeuvre\n:";
    var_dump( $oeuvre );
    echo "\nterm_id\n:";
    var_dump( $term_id );

    Also, could you please add the variable manually like this:

    $term_taxonomy_ids = wp_set_object_terms( 99, 123, 'statut_pret');

    Where “99” is the post ID and “123” is the term ID.

    Thanks 🙂

  • Hi,
    in fact , the problem was the $oeuvre variable, for it is an array
    i had to write :
    $term_taxonomy_ids = wp_set_object_terms( $oeuvre->ID, $term_id, ‘statut_oeuvre’);
    in steadof
    $term_taxonomy_ids = wp_set_object_terms( $oeuvre, $term_id, ‘statut_oeuvre’);
    Thanks for help
    Fred

  • solved

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.