Come ottenere tutti i tag di un custom post type tramite ID

18 mar 2015, 10:36:51
Visualizzazioni: 14K
Voti: 1

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

3
Commenti

@PieterGoosen ho provato la funzione predefinita dei tag di WordPress ma ho bisogno di qualcosa di personalizzato, quindi cerco una soluzione, qui puoi vedere cosa ho fatto nella mia ricerca.

sohan sohan
18 mar 2015 11:53:08

Prima dici "il print_r non mi restituisce nulla." poi dopo il tuo blocco di codice dici "mi sta restituendo i tag", non restituisce nulla o restituisce i tag sbagliati? Stai usando la funzione correttamente per quanto posso vedere, quindi il tuo prossimo passo sarebbe assicurarti che $post->ID sia l'ID che ti aspetti. Il passo successivo è assicurarti che a $post->ID siano assegnati dei tag nel pannello di amministrazione. Infine, controllerei due volte la tassonomia product_tag per assicurarti che sia scritta correttamente.

Howdy_McGee Howdy_McGee
18 mar 2015 15:50:40

wp_get_post_tags non mi restituisce dati mentre wp_get_post_terms mi restituisce un array di dati. Analizzandolo posso ottenere i tag. Qui i tag sono anche di tipo personalizzato "product_tag" per post_type=product.

sohan sohan
19 mar 2015 12:18:46
Tutte le risposte alla domanda 4
0

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.

16 ago 2016 11:07:18
0

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;
    }
7 giu 2019 20:08:54
0

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;
    }
}
11 mar 2019 22:03:51
2
-2
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">&shy;</i>
                </a>
            </div>
        <?php endforeach;
    }
} 
wp_reset_postdata();
3 nov 2015 23:04:51
Commenti

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

Gabriel Gabriel
3 nov 2015 23:33:55

Se sei il "Luis" che ha appena provato a modificare questa domanda, unisci i tuoi account e modificala nuovamente.

cjbj cjbj
16 ago 2016 10:49:43