Wp set post terms not work

My code is not working, it does not add the taxonomy

add_action('init', 'post_test', 10);
function post_test() {
    $empty = get_page_by_title('Produto teste', OBJECT, 'produtos');
    if(empty($empty)) {
        $insert_post = array(
            'post_status' => 'publish',
            'post_type' => 'produtos',
            'post_title' => 'Produto teste',
        );
        $post_id = wp_insert_post($insert_post);
        if ($post_id) {
            wp_set_post_terms($post_id, array('metais'), 'categoria-produtos'); // tag metais id is 108
        }
        add_post_meta($post_id, 'fornecedores', '5948'); // id do fornecedor
        add_post_meta($post_id, '_fornecedores', 'field_52377232eb265');
    }
}

why not work?

1 Answer
1

As stated in the wp_set_post_terms Comment:

This function will only work on the native post type. For a taxonomy on a custom post type use wp_set_object_terms()

wp_set_object_terms

Leave a Comment