2

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.

1
  • What's the other taxonomy? You code doesn't mention it. Commented Apr 3, 2013 at 17:02

2 Answers 2

3

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
    )
)
9
  • thanks for the answer however I can not get it to work. Ive update my question with the edited code.
    – 730wavy
    Commented Apr 8, 2013 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. Commented Apr 8, 2013 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?
    – 730wavy
    Commented Apr 8, 2013 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. Commented Apr 8, 2013 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
    – 730wavy
    Commented Apr 8, 2013 at 19:12
2

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;
     }
}
} ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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