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’m working on a member page where I use a custom post type with a custom taxonomy. My custom post type is called “member” and my custom taxonomy is called “member_groups”.

I want to list all the members but group them together into their respective groups.

So to be clear, I’ve 35 members divided into 9 groups – so instead of making the same query nine times I want to do it once but group them together, so that Member1, Member4 and Member 11 is grouped together in one group, called “Marketing”.

I’m using WP_Query to retrieve all posts under post type member. I’ve tried different attempts but with no successful result.

Any help or suggestions would be much appreciated.

Sincere
- Mestika

share|improve this question
add comment

5 Answers

So, you might consider automating the multiple queries.

First, get the list of terms in your custom taxonomy, using get_terms():

<?php
$member_group_terms = get_terms( 'member_group' );
?>

Then, loop through each one, running a new query each time:

<?php
foreach ( $member_group_terms as $member_group_term ) {
    $member_group_query = new WP_Query( array(
        'post_type' => 'member',
        'tax_query' => array(
            array(
                'taxonomy' => 'member_group',
                'terms' => array( $member_gorup_term->slug ),
                'operator' => 'IN'
            )
        )
    ) );
    ?>
    <h2><?php echo $member_group_term->name; ?><?h2>
    <ul>
    <?php
    if ( $member_group_query->have_posts() ) : while ( $member_group_query->have_posts() ) : the_post(); ?>
        <li><?php echo the_title(); ?></li>
    <?php endwhile; endif; ?>
    </ul>
    <?php
    // Reset things, for good measure
    $member_group_query = null;
    wp_reset_postdata();
}
?>

I can't see anything particularly wrong with this approach, though it may have a limited ability to scale (i.e. if you have hundreds or thousands of members, or member_group terms, you may see performance issues).

share|improve this answer
add comment
up vote 3 down vote accepted

I found a solution by using a custom query and then grouping it with the term name:

SELECT * 
FROM wp_term_taxonomy AS cat_term_taxonomy
INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id
INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID
INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id
WHERE cat_posts.post_status =  'publish'
AND meta.meta_key =  'active'
AND meta.meta_value =  'active'
AND cat_posts.post_type =  'member'
AND cat_term_taxonomy.taxonomy =  'member_groups'

Then by just using a regular foreach query I can just extract the information I want.

But I'm still interested in another way if there is, maybe by using Wordpress' own functions.

share|improve this answer
    
I've just added an alternative method. I tend to shy away from anything that requires raw SQL queries. –  Chip Bennett Jan 25 '12 at 14:32
    
I'm glad to see this marked as the correct answer, even if the query stops working in wordpress if the schema changes at some point... The concept of collecting them all in a single query is the correct answer. Iteration to group the taxonomies in php wont scale nearly as well as this will. –  wowo_999 Nov 12 '13 at 23:03
add comment

even simpler:

$terms = get_terms('tax_name');
$posts = array();
foreach ( $terms as $term ) {
    $posts[$term->name] = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post_type', 'tax_name' => $term->name ));
}

Within the resultant $posts array, each tax term is the key to a nested array containing its posts.

share|improve this answer
add comment

I had this exact need, and Chip's solution worked, except for one thing: 'field' => 'slug' is required.

    foreach ( $service_categories as $category ) {
        $services = new WP_Query( 
            array(
                'post_type'     => 'service',
                'tax_query'     => array(
                    array(
                        'taxonomy'  => 'service_category',
                        'terms'     => array( $category->slug ),
                        'operator'  => 'IN',
                        'get'       => 'all',
                        'field'     => 'slug'
                    )
                )
            ) 
        ); ?>
        <h2><?php echo $category->slug; ?></h2>
        <?php if ( $services->have_posts() ) {  // loop stuff goes here ?>

I also needed the resulting display to be flat, so 'get' => 'all' is set here.

Hopefully this helps somebody else out.

share|improve this answer
add comment
$query = new WP_Query( 
   array ( 
      'post_type' => 'member', 
      'orderby'   => 'meta_value', 
      'meta_key'  => 'member_group' 
   ) 
);

Then when you loop through this query you could just use an if along these lines (in php pseudocode)

$groupName = "";
$counter = 0;
if havePosts: while havePosts: thePost

if( $groupName != post->meta_value )
{
if ($counter > 0)
{
</ul>
}
<h1>A group name</h1>
<ul>
<li>member name</li>
}
else
{
<li>member name</li>
}

endwhile;endif

</ul>

I hope that helps. I think you were making this far more complicated than it needed to be.

More information: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

share|improve this answer
add comment

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.