Support

Account

Home Forums Feature Requests Conditional Logic using Taxonomy field Reply To: Conditional Logic using Taxonomy field

  • Hi I am trying to get either of the two above examples to work and seem to be failing> I am not sure what is going wrong. This is the code

    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_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 != $posted_role ) {
            wp_set_object_terms( $post_id, $term_id->term_id, 'asproducts_cats' );
        }
    }
    add_action('acf/save_post', 'change_post_taxonomy_44582', 20);
    

    Have I done something wrong?