Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am currently using the following code to display a list with links to posts in a specific CPT and taxonomy:

  <?php
$custom_terms = get_terms('videoscategory');

foreach(array($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'product',
        'tax_query' => array(
      'relation' => 'AND',
    array(
        'taxonomy' => 'videoscategory',
        'field' => 'slug',
        'terms' => $custom_term->slug
    ),
    array(
        'taxonomy' => 'product_category',
        'field' => 'slug',
        'terms' => $other_custom_term->slug
    ),

)

     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';
        endwhile;
     }
} ?>

It's working fine like it should, however I want to only display posts that is in both my taxonomies. What do I have to add to do so?

Any help is appreciated, thanks.

share|improve this question
    
What's the other taxonomy? You code doesn't mention it. –  dalbaeb Apr 3 '13 at 17:02
add comment

2 Answers

According to the Codex, here's how you would query posts from several taxonomies:

'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'videoscategory',
        'field' => 'slug',
        'terms' => $custom_term->slug
    ),
    array(
        'taxonomy' => 'yourothertaxonomy',
        'field' => 'slug',
        'terms' => $other_custom_term->slug
    )
)
share|improve this answer
    
thanks for the answer however I can not get it to work. Ive update my question with the edited code. –  mygm26 Apr 8 '13 at 3:21
    
@mygm26 what results do you get? do you get expected results with each taxonomy separately? Check your spelling (for example you have an underscore in one tax name but not the other). Also, what is the value of $other_custom_term? It's not defined right now. –  dalbaeb Apr 8 '13 at 3:37
    
Hello @dalbaeb when using it separately it works as it should, but when trying to use it together nothing shows. The missing underscore is on purpose. The $other_custom_term is product_category. How can I define it and include it in the foreach loop? –  mygm26 Apr 8 '13 at 18:14
    
Well, you are looping over all the terms in videoscategory taxonomy, and your product_category terms are simply not defined inside that loop. You need to figure out how to have terms from both taxonomies available to the query, which is not a WordPress issue, but rather a general code issue. –  dalbaeb Apr 8 '13 at 18:52
    
Oh, Ok. I tried defining it with this - $other_custom_terms = get_terms('product_category'); but Im not sure how to include it in the foreach array –  mygm26 Apr 8 '13 at 19:12
show 4 more comments
up vote 1 down vote accepted

Ok I figured it out!

<?php
$custom_terms = get_terms('your_other_category');
$other_custom_terms = get_terms('your_category');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'product',
        'tax_query' => array(
  'relation' => 'AND',
    array(
        'taxonomy' => 'your_category',
        'field' => 'slug',
        'terms' => $other_custom_term->slug
    ),
            array(
                'taxonomy' => 'your_other_category',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';
        endwhile;
     }
}
} ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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