Come ottenere i post di un termine di tassonomia personalizzata
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;

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"]);

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

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