Group WP_Query by category

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(); ?>

Solutions Collecting From Web of "Group WP_Query by category"

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()).