1

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; ?>
2
  • 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
    Commented Feb 18, 2012 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
    Commented Feb 18, 2012 at 22:58

1 Answer 1

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.

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.