0

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() ?

1 Answer 1

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>';
}

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.