Grouping posts by 2 different taxonomies

22 Views Asked by At

I have the following issue: I have a custom post type (books) with the following taxonomies (1) Categories and (2) Series. Within categories, I have 4 genre sub categories and all books are divided into those 4 sub-categories. Within series, I have 4 different series names and all books that are part of a series are assigned to their series.

The code I have will only display the books organized by category or by series. What I need is to organize by category and then, within the category, by the series.

So it would look like this CATEGORY #1 -Series Name --All posts within the series -Series Name --All posts within the series

CATEGORY #2 -Series Name --All posts within the series -Series Name --All posts within the series

etx, etx.

$custom_post_type = 'books'; // get all books 
$taxonomy = 'series'; // get book series - also works with categories
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) {
wp_reset_query();
$args = array(
    'post_type' => $custom_post_type,
    'showposts' => '5000',
    'meta_key' => 'available_date',
    'orderby' => 'meta_value',
    'tax_query' => array(
          array(
            'taxonomy' => $taxonomy,
            'field' => 'slug',
            'terms' => $term->slug,
               ),

),

);

                    $query = new WP_Query( $args );
                    if ( $query->have_posts() ) {

                        echo '<div><h2>'; echo $term->name.'</h2><ul class="book-container">';
                        
                            while ( $query->have_posts() ) : $query->the_post();

                            echo '';
                                the_title();
                            echo '<br />';

                    endwhile; echo '</ul></div><!-- div -->'; wp_reset_query();
                    }; 
                    };

I have tried adding another foreach but it just repeats all the books under each heading.

0

There are 0 best solutions below