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 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; ?>
share|improve this question
    
There are a few different ways to do this. Can you share the code that you currently have for pulling the categories for each post? It sounds like we may be able to refactor that into something simpler. –  Tom Feb 18 '12 at 22:34
    
well, im sort of all over the place right now. Im just trying whatever. But I posted the basic idea above in my edit –  dtj Feb 18 '12 at 22:58

1 Answer 1

How about doing something like this?

//establish holders
$clients = array();
$media   = array();

//loop through gotten posts
foreach( $clients_query as $k => $v ) {
    //add to clients array
    if( !in_array( get_client( $v ), $clients ) )
        $clients[] = get_client( $v );
    //add to media array
    if( !in_array( get_medium( $v ), $media ) )
        $media[] = get_medium( $v );
}

You will need to replace get_client() and get_medium() with whatever you're using to pull those values (presumably post meta, but you didn't give enough info for me to feel confident in writing that code for you), then you will have an array of clients and an array of media. This same loop would allow you to remove duplicates with unset( $clients_query[$k] )...though if you do that you may want to reindex the array afterwards.

share|improve this answer

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.