Last active
March 25, 2016 17:08
-
-
Save robertpassaro/c5ace8c69e6fb0ee794c to your computer and use it in GitHub Desktop.
WP template code to query a post type according to terms from a custom taxonomy -- and loop through to divide the posts by term
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="page-content"> | |
<?php | |
// Specify the taxonomy and post type for the queries. | |
$fq_post_type = 'xxxxpost_typexxxxx'; | |
$fq_taxonomy = 'xxxtaxonomyxxx'; | |
$term_args = array( | |
'orderby' => 'title', | |
'order' => 'ASC', | |
); | |
$custom_tax_terms = get_terms( $fq_taxonomy, $term_args ); | |
if ( !empty( $custom_tax_terms ) ) { | |
foreach ( $custom_tax_terms as $tax_term ) { | |
$term_heading = $tax_term->name; | |
$query_term = $tax_term->slug; | |
$args = array( | |
'post_type' => $fq_post_type, | |
'posts_per_page' => -1, | |
'orderby' => 'title', | |
'order' => 'ASC', | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'genre', | |
'field' => 'slug', | |
'terms' => $query_term, | |
), | |
), | |
); | |
$fq_tax_query = new WP_Query( $args ); | |
if ( $fq_tax_query->have_posts() ) : | |
echo '<h3 id="' . $query_term . '" class="book-archive-title">' . $term_heading . '</h3>'; | |
echo '<section class="genre-group">'; | |
while ( $fq_tax_query->have_posts() ) : $fq_tax_query->the_post(); | |
get_template_part( 'content', get_post_type() ); | |
endwhile; | |
echo '</section>'; | |
endif; | |
} | |
} | |
wp_reset_postdata(); ?> | |
</div><!-- .page-content --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment