Come ottenere i post di un termine di tassonomia personalizzata

29 giu 2015, 21:45:38
Visualizzazioni: 20.3K
Voti: 9

Spero che qualcuno possa aiutarmi:

Ho un Custom Post Type (Film) con la sua tassonomia personalizzata (Produttore), questa tassonomia ha i suoi propri termini, per esempio 'WarnerBros'.

Come posso ottenere tutti i post del mio termine (WarnerBros)?

Ho questo codice ma non funziona ancora.

$args = array(
   'post_type' => 'movie',
    'tax_query' => array(
        array(
            'taxonomy' => 'producer',
            'field'    => 'slug',
            'terms'    => 'WarnerBros',
        ),
    ),
);
$query = new WP_Query( $args );

Dopo aver lavorato sul codice ho risolto il problema, condividerò il mio codice per chi ha lo stesso problema:

 
$type = 'Movie';  // Nome del Custom Post Type
$tag =  'WarnerBros'; // Il tuo Termine

$args = array( 'post_type' => $type, 'paged' => $paged, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'tax_query'=>array( array( 'taxonomy'=>'Producer', //Nome della Tassonomia 'field'=>'slug', 'terms'=>array($tag) )) );

$loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); if(is_object_in_term($post->ID,'Taxonomy_Name','Your_Term')) // Produttore e WarnerBros {

echo '<div id="YourID">'; echo the_title(); echo '</div>'; } endwhile;

3
Commenti

Ho provato con molti esempi ma niente funziona.

Ebenizer Pinedo Ebenizer Pinedo
29 giu 2015 22:19:44

Per favore non postare blocchi di codice nei commenti, invece clicca "modifica" nella tua domanda e poi clicca {} per inserire il tuo codice in modo formattato. Questo rende più facile per gli utenti vedere cosa hai provato e magari modificarlo per risolvere il tuo problema!

Howdy_McGee Howdy_McGee
29 giu 2015 22:21:39

Scusa, ora è sistemato ;)

Ebenizer Pinedo Ebenizer Pinedo
29 giu 2015 22:25:30
Tutte le risposte alla domanda 3
2

Questa domanda ha diverse risposte in questa specifica domanda su Wordpress, che potrebbero essere d'aiuto:

Mostra tutti i post in un custom post type, raggruppati per una tassonomia personalizzata

Personalmente ho usato questo metodo che ha funzionato bene per me:

$terms = get_terms('tax_name');
$posts = array();
foreach ( $terms as $term ) {
    $posts[$term->name] = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post_type', 'tax_name' => $term->name ));
}

Adattandolo al tuo scenario, questo dovrebbe funzionare:

$terms = get_terms('producer');
$posts = array();
foreach ( $terms as $term ) {
    $posts[$term->name] = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'movie', 'tax_name' => $term->name ));
}

Ora puoi ottenere i tuoi post:

print_r($posts["WarnerBros"]);
29 giu 2015 22:30:37
Commenti

Non funziona per me, forse perché lo snippet non ha il nome del termine.

Ebenizer Pinedo Ebenizer Pinedo
29 giu 2015 23:03:54

Ho modificato la risposta per adattarla alle tue variabili. Non aspettarti che le risposte abbiano esattamente le stesse tue variabili.

marcovega marcovega
29 giu 2015 23:07:44
1

Prova così

$args = array(
    'post_type' => 'movie',
    'tax_query' => array(
        array(
            'taxonomy' => 'producer',
            'field'    => 'slug',
            'terms'    => 'WarnerBros',
        ),
    ),
);
$query = new WP_Query( $args );

Vedi di più su WordPress Codex

29 giu 2015 22:01:11
Commenti

Sembra che non funzioni, dovrei usare il mio term slug? O forse chiamare un'altra funzione come: <?php (have_posts()) : the_post(); ?>

Ebenizer Pinedo Ebenizer Pinedo
29 giu 2015 22:18:17
0

Supponiamo che tu abbia un custom post type plays e sotto la tassonomia genre vuoi trovare tutti i post con la categoria commedia

$args = array(
        'post_type' => 'plays', /*Tipo di post (plays)*/
        'tax_query' => array(
            array(
                'taxonomy' => 'genre', /*Tassonomia da cercare (genre)*/
                'field'    => 'slug',
                'terms'    => 'comedy', /*Cerca la categoria (commedia)*/
            ),
        ),
    );
    $query = new WP_Query( $args );
19 nov 2017 18:11:04