Ottenere l'ID della categoria da un ID post di un custom post type
3 mag 2014, 10:22:17
Visualizzazioni: 41.1K
Voti: 6
Voglio ottenere l'ID della categoria da un ID post di un tipo personalizzato. Ho l'ID del post, ma non riesco a ottenere l'ID della sua categoria.
Ho utilizzato molti codici ma non funzionano, forse a causa del custom post type.
$category = get_the_category( $post->ID );
Qualche suggerimento?

Aman
61
Tutte le risposte alla domanda
3
0
wp_get_post_categories
può ottenere solo le categorie dei POST, non le categorie di un custom post, prova invece questo:
$category = get_the_terms( $post->ID, 'custom-taxonomy-here' );
foreach ( $category as $cat){
echo $cat->name;
}
Controlla questo link

GIPSSTAR
271
19 ago 2015 16:30:53
1
Il tuo custom post type supporta la tassonomia standard delle categorie? Se la categoria utilizzata nel tuo custom post type è una tassonomia personalizzata e non la tassonomia standard delle categorie, dovresti usare get_the_terms()
invece di get_the_category()
.
$categories = get_the_terms($post->ID, "my-custom-taxonomy");

cybmeta
20.6K
3 mag 2014 11:05:58
0
questa soluzione funziona per me:
global $wpdb;
// ottieni tutti gli ID delle categorie basati sull'ID del post
$result = $wpdb->get_results( " select term_taxonomy_id from " . $wpdb->prefix . "term_relationships where object_id = '" . $post_id . "' " );
$cats_ids_array = [];
foreach ( $result as $c ) {
$cats_ids_array[] = $c->term_taxonomy_id;
}

Pawel Klopotowski
11
9 ott 2018 11:50:06
Domande correlate