WordPress List Posts by Taxonomy

Published 20th September 2012

Below is a function I created recently and thought it might be worth sharing. It lists the taxonomies of a given post type with a sub-list of the posts within that taxonomy.

function list_posts_by_taxonomy( $post_type, $taxonomy, $get_terms_args = array(), $wp_query_args = array() ){ $tax_terms = get_terms( $taxonomy, $get_terms_args ); if( $tax_terms ){ foreach( $tax_terms as $tax_term ){ $query_args = array( 'post_type' => $post_type, "$taxonomy" => $tax_term->slug, 'post_status' => 'publish', 'posts_per_page' => -1, 'ignore_sticky_posts' => true ); $query_args = wp_parse_args( $wp_query_args, $query_args ); $my_query = new WP_Query( $query_args ); if( $my_query->have_posts() ) { ?>

slug; ?>” class=”tax_term-heading”><?php echo $tax_term->name; ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>* [" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>](<?php the_permalink() ?) <?php } wp_reset_query(); } } }

You can then call the function wherever you want to output the list:

list_posts_by_taxonomy( 'my_post_type', 'my_post_type_taxonomy' );