0

I have two custom taxonomies called "custom-category" and "file_type". I am using the below code to list all the terms of "file_type" along with their associated posts. Like so:

File_Type Term 1

  • post
  • post
  • post

File_Type Term 2

  • post
  • post
  • post

and so on...

My problem is that I need to add another condition to this. I want to only show the posts if they also have a specific term in "custom-category" taxonomy. So, for each file_type term, I want it to only show posts that have that term AND have the term "learning-units" in the custom-category taxonomy (term ID is 9).

I'm so confused. Here is my working code for listing posts by file_type term. How do I add that extra condition?

<?php

    $categories = get_terms('file_type');

    foreach ( $categories as $category ) :

    ?>

    <h2><?php echo $category->name; ?></h2>

    <?php

        $posts = get_posts(array(
          'post_type' => 'product',
          'taxonomy' => $category->taxonomy,
          'term'  => $category->slug
          ));

// Here's the second, nested foreach loop that cycles through the posts associated with this category

        foreach($posts as $post) :
          setup_postdata($post); 
        ?>


           <div class="product">

           <h3><span><?php the_title(); ?></span></h3>
           <?php the_post_thumbnail('product'); ?>
           <a href="<?php the_permalink(); ?>" class="button">Learn More</a>


          </div>


        <?php endforeach; ?>


 <?php endforeach; ?>
0

You can directly add 'taxonomy_name'=>'term_slug' in your current code.

 $posts = get_posts(array(
          'post_type' => 'product',
          'file_type'=> $category->slug,
          'custom-category' => 'learning-units'
          ));

For more reference check WP_Query Taxonomy Parameters

3
  • Facepalm! I had tried some variations of that in a previous iteration, which didn't work for other reasons. I forgot to come back to that once I got the main loop working. THANK YOU. This worked exactly as needed. – LBF May 9 '14 at 5:28
  • Note that that type of taxonomy argument was deprecated as of Version 3.1 in favor of 'tax_query'. – Milo May 9 '14 at 5:34
  • Ah, ok. Thanks for that info @Milo. I assumed this way was newer because it was less code. But I will try your solution below as well. – LBF May 9 '14 at 14:09
1

Use a tax_query to set multiple taxonomy arguments. See WP_Query Taxonomy Parameters for more info.

$posts = get_posts(array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => $category->taxonomy,
            'field' => 'slug',
            'terms' => $category->slug
        ),
        array(
            'taxonomy' => 'custom-category',
            'field' => 'id',
            'terms' => 9
        )
));

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.