Support

Account

Home Forums Backend Issues (wp-admin) Use ACF/Save Post Action to update a Custom Taxonomy

Solving

Use ACF/Save Post Action to update a Custom Taxonomy

  • Hi

    A brief explanation; I have a post type called products, with a taxonomy which is product type. I have created a post type check box field with conditional logic so when the user checks the type, certain fields become available. I would use a taxonomy field, but ACF, as of yet, dose not support conditional logic on this field type. I believe the work around is to use the acf/save post action to update the taxonomy. On searching this forum, I found a bit of code which I believe will do the trick. I have modified it to work with my site, but it is not working.

    function change_post_taxonomy_44582( $post_id ) {
        // bail if no ACF data
        if ( empty($_POST['acf']) ) {
            return;
        }
        // get term id from $post_id (only 1 value is allowed, so it returns 1 value only)
        $stored_cat = wp_get_post_terms($post_id, 'asproducts_cats');
        // get submitted value from acf form
        $posted_cat = $_POST['acf']['field_59154e83efb0e'];
        // get term_id for the submitted value
        $term_id    = get_term_by( 'name', $posted_cat, 'asproducts_cats' );
        // if stored value is not equal to posted value, then update terms
        if ( $stored_cat != $posted_cat ) {
            wp_set_object_terms( $post_id, $term_id->term_id, 'asproducts_cats' );
        }
    }
    add_action('acf/save_post', 'change_post_taxonomy_44582', 20);

    For the life of me I can not spot what is wrong, is anybody able to help and point me in the right direction.

    Many Thanks

    Keith

  • @mrkeithy did you ever figure this one out? exactly what i’m looking to do. thank you!

  • yes I did, here is the code I am using.

    function change_post_taxonomy_44582( $post_id ) {
       
        if ( empty($_POST['acf']) ) {
            return;
        }
        
         // get term id from $post_id (only 1 value is allowed, so it returns 1 value only)
        $stored_role = wp_get_post_terms($post_id, 'asproducts_cats');
        // get submitted value from acf form
        $posted_roles = $_POST['acf']['field_59154e83efb0e'];
        // get term_id for the submitted value
       
        $term_id    = get_term_by( 'name', $posted_roles, 'asproducts_cats' );
        // if stored value is not equal to posted value, then update terms
       
        if ( $stored_role[0] != $posted_roles ) {
            wp_set_object_terms( $post_id, $term_id->term_id, 'asproducts_cats' );
        }
    }
    add_action('acf/save_post', 'change_post_taxonomy_44582', 20);

    Also have a look here https://support.advancedcustomfields.com/forums/topic/conditional-logic-using-taxonomy-field/page/4/

    A nice chap helped me out with this. Shout if you are still having issues

    Keith

  • Thanks so much Keith! Much appreciated.

  • hi @mrkeithy,
    Would you please take a look at this topic please?
    https://support.advancedcustomfields.com/forums/topic/update-taxonomy-of-posts-selected-by-acf-relation-field/#post-123227

    i’m not sure what i’m missing i appreciate it if you could give me a hint.

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

The topic ‘Use ACF/Save Post Action to update a Custom Taxonomy’ is closed to new replies.