admin管理员组

文章数量:1125905

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.

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.

Share Improve this question asked Jan 17, 2024 at 3:47 K. GloverK. Glover 212 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 
}
}
}

本文标签: custom taxonomyGrouping posts by 2 different taxonomies