Come ottenere tutti i tag di un custom post type tramite ID
Domanda semplice: come ottenere tutti i tag di un custom post type tramite ID? post_type=product.
Ho provato con http://codex.wordpress.org/Function_Reference/wp_get_post_tags nel mio post loop e il print_r non restituiva nulla.
Quindi ho provato questo:
$term_list = wp_get_post_terms($post->ID, 'product_tag', array("fields" => "all"));
print_r($term_list);
e ora ottengo i tag nel mio print_r($term_list);
Grazie

Approccio al loop: solitamente il file archive-{custom_post}.php.
PRIMO:
custom_post_plural Rappresenta un gruppo di custom post di un certo tipo.
Esempio di custom_post_plural: prodotti
custom_post_singular Rappresenta un singolo custom post type.
Esempio di custom_post_singular: prodotto
SECONDO:
La variabile $args_custom_post_plural contiene i parametri della WP_Query.
La variabile $custom_post_plural è l'esecuzione della query.
Ho usato la variabile $custom_post_plural_output per iterare il contenuto del WP_Object, specificatamente con il termine posts, rendendo il suo contenuto "array friendly".
Come puoi vedere ho parzialmente utilizzato le istruzioni di Ahmad per un'iterazione annidata.
$args_custom_post_plural=array(
'post_type' => 'custom_post_singular',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'order_by' =>'id',
'order' => 'ASC'
);
$custom_post_plural = new WP_Query($args_custom_post_plural);
$custom_post_plural_output = $custom_post_plural->posts;
for ($i=0; $i < count($custom_post_plural_output); $i++) {
$tags = wp_get_post_tags($custom_post_plural_output[$i]);
$buffer_tags ='';
foreach ( $tags as $tag ) {
$buffer_tags .= $tag->name . ',';
}
}
echo $buffer_tags;
FINE:
FYI Se vuoi usare questo codice in un file single-{custom_post}.php, puoi usare il seguente codice:
$tags = wp_get_post_tags($post->ID);
foreach ( $tags as $tag ) {
$buffer_tags .= $tag->name . ',';
}
echo $buffer_tags;
Dato che devi avere un post collegato per visualizzare qualsiasi cosa.
Buon coding.
PS. @cjbj Perché diavolo hai cancellato la mia modifica, c'era qualcosa di sbagliato o cosa? Gestione terribile qui, e molto maliziosa dato che non posso rispondere a un commento a causa dei miei punti reputazione.

Se hai bisogno di ottenere i tag tramite l'ID del post, puoi utilizzare la seguente funzione. Questa funzionerà ovunque poiché il metodo si basa su una query al database.
function sc_tf_get_tags_as_array($post_id){
global $wpdb;
$tbl_terms = $wpdb->prefix . "terms";
$tbl_term_relationships = $wpdb->prefix . "term_relationships";
$sql = "SELECT name FROM $tbl_terms WHERE term_id in (SELECT term_taxonomy_id FROM $tbl_term_relationships WHERE object_id='$post_id');";
$results = $wpdb->get_results($sql);
if($results){
foreach($results as $row){
$tags_list[] = $row->name;
}
}
return $tags_list;
}

wp_get_post_tags funziona solo per i post, non per altri tipi di contenuto. Se dai un'occhiata a /wp-includes/post.php vedrai che chiama la funzione wp_get_post_terms con $taxonomy impostato a 'post_tag':
function wp_get_post_tags( $post_id = 0, $args = array() ) {
return wp_get_post_terms( $post_id, 'post_tag', $args );
}
Per i tag dei prodotti o altre tassonomie puoi usare get_the_terms() invece:
$tags = get_the_terms( $prod_id, 'product_tag' );
$tags_names = array();
if ( ! empty( $tags ) ) {
foreach ( $tags as $tag ) {
$tags_names[] = $tag->name;
}
}

global $post;
$tags = wp_get_post_tags( $post->ID );
if ( $tags ) {
foreach ( $tags as $tag ) {
$tag_arr .= $tag->slug . ',';
}
$args = array(
'tag' => $tag_arr,
'post_per_page' => 10,
'post__not_in' => array( $post->ID ),
'post_type' =>'post_type_name'
);
$related_posts = get_posts( $args );
if ( $related_posts ) {
foreach ( $related_posts as $post ) : setup_postdata( $post ); ?>
<div class="swiper-slide">
<a class="hover-effect image-holder" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<i class="icon-circle icon-circle--thin fa fa-arrow-right">­</i>
</a>
</div>
<?php endforeach;
}
}
wp_reset_postdata();

Le risposte dovrebbero essere più di un semplice frammento di codice. Per favore, invia una modifica e spiega perché questa soluzione potrebbe funzionare.
