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 badges1 Answer
Reset to default 1After 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
版权声明:本文标题:custom taxonomy - Grouping posts by 2 different taxonomies 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736678872a1947323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论