Created
November 3, 2017 19:47
-
-
Save conorbarclay/ff446d6e7307012ed52c963405dbab09 to your computer and use it in GitHub Desktop.
WordPress nested taxonomy + custom post type loop example
This file contains hidden or 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
<?php | |
$post_type = 'post_type'; | |
$taxonomy = 'taxonomy'; | |
$terms = get_terms( $taxonomy ); | |
?> | |
<?php | |
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { | |
foreach ( $terms as $term ) { ?> | |
<h2><?php echo $term->name; ?></h2> | |
<?php | |
$args = array( | |
'post_type' => $post_type, | |
'posts_per_page' => - 1, | |
'tax_query' => array( | |
array( | |
'taxonomy' => $taxonomy, | |
'field' => 'slug', | |
'terms' => $term->slug, | |
'operator' => 'IN' | |
) | |
), | |
); | |
$q = new WP_Query( $args ); | |
if ( $q->have_posts() ) : ?> | |
<?php while ( $q->have_posts() ) : $q->the_post(); ?> | |
<?php the_title(); ?> | |
<?php endwhile; ?> | |
<?php endif; ?> | |
<?php wp_reset_query(); ?> | |
<?php } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment