I have 2 separate groups of Categories that a post can be classified in: Client & Medium.
I'm trying to display all the Clients (if they have posts), and any Mediums that those posts may belong to.
The problem I'm running into is this: Lets say a 3 posts are associated with Client A. When I'm in the loop, and gathering all the Categories for each post, I get 'Client A' 3 times, instead of just once (as I want it).
The same is true with the Medium category as well.
I want to display only:
a) the unique / distinct Clients (that have posts)
b) the mediums that those posts are categorized by, per client.
What's the best way of going about this? If I'm in the Loop, is there a way to only pull unique Categories? Or do I have to gradually put them all in an array, and check that they don't exist before adding a new one?
How about using some sort of Group By clause?
Here's some of the code I'm using:
<?php $args = array('cat' => 4 ); $clients_query = new WP_Query( $args ); ?> <?php while ( $clients_query->have_posts() ) : $clients_query->the_post(); ?> <?php $categories = get_the_category(); ?> <?php foreach($categories as $category) : ?> <?php if (cat_is_ancestor_of(4, $category)) : ?> /* 4 is the Client Cat -- so stick this in an array */ <?php elseif (cat_is_ancestor_of(5, $category)) /* 5 is the Medium Cat -- so stick this in a seperate array maybe? <?php endif; ?> <?php endforeach; ?> <?php endwhile; ?>