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.

OK, here's my setup:

Custom post type called "issues" (for a magazine) Posts with custom meta field matching the post ID of the corresponding issue.

When I'm on a single "issue" post page, I want to query all the related posts, and display them grouped by their associated category. I have the post query working, I just can't seem to get my head around the category grouping.

here's my query

   <?php
    global $post;

    // List posts by the terms for a custom taxonomy of any post type   
    $current    = get_the_ID($post->ID);
    $args = array(
        'post_type'         => 'post',
        'post_status'       => 'publish',
        'posts_per_page'    => -1,
        'orderby'           => 'title',
        'meta_key'          => '_rkv_issue_select',
        'meta_value'        => $current
    );

    $issue_cats = new WP_Query($args);

    if( $issue_cats->have_posts() ) :
    ?>
    <ul>
    <?php while ( $issue_cats->have_posts() ) : $issue_cats->the_post(); ?>

        <li><?php the_title(); ?></li>
    <?php endwhile; // end of loop ?>
    <?php else : ?>
    <?php endif; // if have_posts() ?>
    </ul>
    <?php wp_reset_query(); ?>
share|improve this question

1 Answer 1

up vote 7 down vote accepted

You could look at modifying the WP_Query with a SQL command to group them, but that's a bit beyond my current MySQL, however, I've always done it by running a foreach on the taxonomy itself with this http://codex.wordpress.org/Function_Reference/get_categories

Here's some sample code:

<?php
    global $post;

    $current = get_the_ID($post->ID);
    $cargs = array(
        'child_of'      => 0,
        'orderby'       => 'name',
        'order'         => 'ASC',
        'hide_empty'    => 1,
        'taxonomy'      => 'category', //change this to any taxonomy
    );
    foreach (get_categories($cargs) as $tax) :
        // List posts by the terms for a custom taxonomy of any post type   
        $args = array(
            'post_type'         => 'post',
            'post_status'       => 'publish',
            'posts_per_page'    => -1,
            'orderby'           => 'title',
            'meta_key'          => '_rkv_issue_select',
            'meta_value'        => $current,
            'tax_query' => array(
                array(
                    'taxonomy'  => 'category',
                    'field'     => 'slug',
                    'terms'     => $tax->slug
                )
            )
        );
        if (get_posts($args)) :
    ?>
        <h2><?php echo $tax->name; ?></h2>
        <ul>
            <?php foreach(get_posts($args) as $p) : ?>
                <li><a href="<?php echo get_permalink($p); ?>"><?php echo $p->post_title; ?></a></li>
            <?php endforeach; ?>
        </ul>
    <?php 
        endif;
    endforeach; 
?>

This will run through every category with posts (hide_empty is set to true) and does a get_posts on that (and also checks to make sure it has posts before outputting anything).

Wasn't sure what you wanted for a header to separate the groupings so I used an h2 and added a link to the listing as well.

I changed it to get_posts because I've found it to be more efficient as it doesn't override the global $post variable (less database calls, less use of wp_reset_query()).

share|improve this answer
    
This is sort of unefficient way to do it, if you have a lot of categories fetched, since foreach category you do a separate get_posts call which calls the db each time. –  Maor Barazany Oct 6 '12 at 17:09
2  
So what is the optimal approach @MaorBarazany? –  lkraav Jan 26 '13 at 2:02
1  
Thanks for this answer, I was bangin' my head on the wall to find a way to group results for taxonomies... –  Warface Sep 3 '13 at 14:38

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.