Skip to main content
4 of 5
deleted 129 characters in body
Chris Pink
  • 734
  • 1
  • 6
  • 21

Cross referencing taxonomies

I have two taxonomies for categories of custom post type 'items'; 'type' and 'group'. They are not exclusive so one hierarchical taxonomy won't work although to an extent, as you'll see, they are hierarchical. So, for instance, I have a group 'group1' which may be in more than one type.

What I need to do is produce a taxonomy page 'type1' that list all the 'groups' with 'type1. It's easy enough to produce a list of posts in 'group1' but I don't know how to list the 'groups' that contain 'items' with type 'type1'.

I can see the logic but I'm struggling to produce the code. As these questions work better with some code, I'll update as I go.

This is on a page taxonomy-type.php;

// First get the type
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$type = $term->slug;
// loop through all 'type1' posts 
while ( have_posts() ) : the_post();
   // get all the terms for 'group'
   $group_terms = get_terms( 'group' );
   //print_r($group_terms);
   // loop through all the terms;
   foreach ($group_terms as $group_term) :
     // if this post in in our taxonomy list it
     if (has_term($group_term, 'group') {
       echo"<li>".$group_term->name."</li>";
     }
   endforeach;
endwhile;

Produces the term followed by the item (much the same as the taxonomy archive would without my help!)

Chris Pink
  • 734
  • 1
  • 6
  • 21