9

I hope someone will help me:

I have a Custom Post Type (Movie) with its custom taxonomy (Producer), this taxonomy has its own terms, for example 'WarnerBros'.

How I can get all the posts of my term (WarnerBros)?

I have this but doesn't work yet.

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

After playing with the code I resolved the problem, I will share my code for someone with the same issue:

 
$type = 'Movie';  // Custom Post Type Name
$tag =  'WarnerBros'; // Your Term

$args = array( 'post_type' => $type, 'paged' => $paged, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'tax_query'=>array( array( 'taxonomy'=>'Producer', //Taxonomy Name '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')) // Producer and WarnerBros {

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

3
  • I have been trying with to many examples but anything works. Jun 29, 2015 at 19:19
  • Please don't post code blocks in comments, instead click "edit" in your question and then click the {} to enter your code in a formatted way. This makes it easier for users to see what you've tried and can maybe modify it to fix your issue!
    – Howdy_McGee
    Jun 29, 2015 at 19:21
  • 1
    Sorry, now is fixed ;) Jun 29, 2015 at 19:25

3 Answers 3

2

This question has different answers in this specific Wordpress question, they may be of help:

Display all posts in a custom post type, grouped by a custom taxonomy

Personally I used this method that worked for me just fine:

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

Editing it to your scenario this should work:

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

Now you can get your posts:

print_r($posts["WarnerBros"]);
2
  • Doesn't work for me, maybe because the snippet doesn't have the term name. Jun 29, 2015 at 20:03
  • I have edited the answer to meet your variables. Don't expect too much that the answers have exactly your same variables.
    – marcovega
    Jun 29, 2015 at 20:07
1

Try like this

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

See more at wordpress codex

1
  • It seems that doesn't work, should I to use my term slug? Or maybe call other function like: <?php (have_posts()) : the_post(); ?> Jun 29, 2015 at 19:18
1

Let's say that you have a custom post type plays and under the taxonomy genre you want to find all the posts with the category comedy

$args = array(
        'post_type' => 'plays', /*Post type (plays)*/
        'tax_query' => array(
            array(
                'taxonomy' => 'genre', /*Taxonomy to search (genre)*/
                'field'    => 'slug',
                'terms'    => 'comedy', /*Search category for (comedy)*/
            ),
        ),
    );
    $query = new WP_Query( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.