I have a custom post type with custom taxonomy and 3 differents terms.
I'm trying to build a page with this structure :
Term 1
- Post tagged with term 1
- Post tagged with term 1
Term 2
- Post tagged with term 2
- Post tagged with term 2
- Post tagged with term 2
Term 3
- Post tagged with term 3
Etc...
What is the best way to achieve that ? has_term() ?
I have a custom post type with custom taxonomy and 3 differents terms.
I'm trying to build a page with this structure :
Term 1
- Post tagged with term 1
- Post tagged with term 1
Term 2
- Post tagged with term 2
- Post tagged with term 2
- Post tagged with term 2
Term 3
- Post tagged with term 3
Etc...
What is the best way to achieve that ? has_term() ?
Share Improve this question asked Jul 10, 2020 at 8:31 BakuraBakura 11 Answer
Reset to default 0$terms = get_terms('CUSTOM_TAXONOMY', ['hide_empty' => true]);
foreach( $terms as $term ){
echo '<section>';
echo '<h1>' . $term->name . '</h1>';
$posts = get_posts([
'post_type' => 'CUSTOM_POST_TYPE'
'tax_query' => [
[
'taxonomy' => 'CUSTOM_TAXONOMY',
'field' => 'term_id',
'terms' => $term->term_id
]
]
]);
echo '<ul>';
foreach($posts as $post){
echo sprintf('<li><a href="%s">%s</a></li>', get_permalink($post), $post->post_title);
}
echo '</ul>';
echo '</section>';
}