Skip to content

Instantly share code, notes, and snippets.

@conorbarclay
Created November 3, 2017 19:47
Show Gist options
  • Save conorbarclay/ff446d6e7307012ed52c963405dbab09 to your computer and use it in GitHub Desktop.
Save conorbarclay/ff446d6e7307012ed52c963405dbab09 to your computer and use it in GitHub Desktop.
WordPress nested taxonomy + custom post type loop example
<?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