How to List All Posts in a Custom Taxonomy Separated by Terms

In this post I will demonstrate how to list all posts from a specific custom taxonomy, separated by its terms. In this example, we will use “movie” as custom post type, “genre” as custom taxonomy, and the terms for this taxonomy: “Action”, “Comedy”, and “Drama.” The loop will output something like this:

Action

Fast & Furious 6
Skyfall
The Avengers

Comedy

Superbad
The Hangover
Ted

Drama

Flight
The Notebook
The Shawshank Redemption

Note: each movie title represents a “movie” custom post.

The Loop

 $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
        );
    
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo $tax_term->name;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            

I hope this helps you on your next WordPress project that uses custom post types and custom taxonomies.

Posted in WordPressTagged ,

Ready to get started?

Get a Free Estimate