Wp set post terms non funziona
Il mio codice non funziona, non aggiunge la tassonomia
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'); // il tag metais ha id 108
}
add_post_meta($post_id, 'fornecedores', '5948'); // id del fornitore
add_post_meta($post_id, '_fornecedores', 'field_52377232eb265');
}
}
perché non funziona?

Come affermato nel wp_set_post_terms
Commento:
Questa funzione funzionerà solo sul tipo di post nativo. Per una tassonomia su un tipo di post personalizzato, utilizza wp_set_object_terms()

Grazie fratello, questo codice funziona! : wp_set_object_terms($post_id, array('metais'), 'categoria-produtos', true); // l'id del tag metais è 108
grazie grazie

Non è più così. Per WP 5+ puoi usare entrambi anche per i custom post type.

Ora il Codex dice: Questa funzione funzionerà con qualsiasi tipo di post. In alternativa, si può usare wp_set_object_terms().

@ToleaBivol Non posso confermare, ho perso 4 ore cercando di capire perché la tassonomia personalizzata non veniva impostata, ho scoperto che è dovuto alla differenza tra wp_set_post_terms e wp_set_object_terms
