1

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

Extra eyes would be wonderful and so appreciated! I am hoping it's something simple I am overlooking.

1 Answer 1

1

After much searching, and lots of help here (thanks again, Sally CJ), this the code I ended up using.

$category_terms = get_terms(array('taxonomy' => 'categories'));
$series_terms = get_terms(array('taxonomy' => 'series'));
$custom_post_type = 'cpt'; 

foreach( $category_terms as $category_term ) {
foreach ($series_terms as $series_term) {
     
        
    $args = array(
        'post_type' => $custom_post_type,
      'showposts' => '5000',
      'meta_key' => 'available_date',  
        'tax_query' => array(
            'relation' => 'AND',
                array(
                    'taxonomy' => 'categories',
                    'field' => 'id',
                    'terms' => $category_term->term_id,
                    
                ),
            
                array(
                    'taxonomy' => 'series',
                    'field' => 'id',
                    'terms' => $series_term->term_id,
                    'operator' => 'IN'
                ),
          
  ),
  
  );
 $query = new WP_Query( $args );
  if ( $query->have_posts() ) {
 //do stuff here 
}
}
}
1
  • I've just deleted my answer, but I'm glad the code helped you. 😃 And if you had told me that the other taxonomy was not category and instead categories, then we would have solved the issue long ago.. 🤭 Anyway, once again, showposts is not a valid parameter for WP_Query, but even if you had used posts_per_page, keep in mind that 5000 is huge and can hurt your website performance - the recommended practice is using 100 or lower, and use pagination if you need to fetch more results. 🙂
    – Sally CJ
    Commented Jan 26 at 5:24

Your Answer

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

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