Wp set post terms non funziona

17 set 2013, 22:00:40
Visualizzazioni: 22.2K
Voti: 4

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?

0
Tutte le risposte alla domanda 1
5
22

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()

wp_set_object_terms

17 set 2013 22:11:23
Commenti

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

Raank Raank
17 set 2013 22:17:21

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

Anatol Bivol Anatol Bivol
10 feb 2019 11:23:21

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

Anatol Bivol Anatol Bivol
10 feb 2019 11:29:13

@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

n.r. n.r.
23 ago 2020 00:05:26

Classico codice di qualità Wordpress.

marek.m marek.m
8 feb 2021 10:16:16