5

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

1 Answer 1

7

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

4
  • 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. Oct 6, 2012 at 17:09
  • 4
    So what is the optimal approach @MaorBarazany?
    – lkraav
    Jan 26, 2013 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, 2013 at 14:38
  • This works for me. Caching can help if performance is bad due to too many queries. Dec 23, 2016 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.