1

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!)

  • if 'type' and 'group' are 2 taxonomies, there is no group with type1. or maybe you added code to do that ? or you want to search groups which are connected to items which are associated to type1 ? – mmm May 18 '18 at 9:52
  • Please feel free to edit the title if it can make more sense – Chris Pink May 18 '18 at 10:08
  • @mmm I want to list groups in type1 if they exist (eventually all types will be none-empty) it's items that can be in group1 and type1 (and must be in at least one of each) but I need to list the groups not the items - does that make sense? – Chris Pink May 18 '18 at 11:44
1

Well I got there, my public brain functions for all to see, hopefully someone somewhen will pass this was and find it useful.

The code;

// First get the type
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$type = $term->slug;
// we need to do some comparisons, here's the variable
 $this_instance = "";
// loop through all 'type1' posts 
while ( have_posts() ) : the_post();
 $group_terms = get_terms( 'group' );
 //print_r($group_terms);
 // then loop through all terms for this post
 foreach ($group_terms as $group_term) :
   if (has_term($group_term, 'group')) {
     $first_instance = $group_term->slug;
     // if this is the first do something
     if ($this_instance !== $first_instance) { ?>
       <li><?= $group_term->name; ?></li>
        <? $this_instance = $first_instance;
      }
    }
  endforeach;
endwhile;

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.