Skip to content

Instantly share code, notes, and snippets.

@anythinggraphic
Last active October 18, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anythinggraphic/f2ac7ba03b34ac65c38b13f2334d2feb to your computer and use it in GitHub Desktop.
Save anythinggraphic/f2ac7ba03b34ac65c38b13f2334d2feb to your computer and use it in GitHub Desktop.
Within your custom loop, get all taxonmies for a custom post type (CPT) and display each post within those taxonomies.
<?php
/* @link TBA
/* Within your custom loop, get all taxonomies for a custom post type (CPT) and display each post within those taxonomies
----------------------------------------------------------------------------------------*/
$post_type = 'your_cpt_name';
// Get all of the taxonomies for this post type
$taxonomies = get_object_taxonomies((object) array( 'post_type' => $post_type )); ?>
<div class="wrap">
<?php foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the posts assigned to it
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$args = array(
'taxonomy' => $taxonomy,
'term' => $term->slug,
'order' => 'ASC',
'posts_per_page' => -1,
);
$loop = new WP_Query($args);
if ($loop->have_posts()) : ?>
<div class="row">
<h4 itemprop="headline">
<?php echo $term->name; ?>
</h4>
<div class="one-third-wrapper">
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<article <?php post_class('one-third') ?>>
<a href="<?php echo get_the_permalink(); ?>">
<?php if (has_post_thumbnail()) {
the_post_thumbnail();
} ?>
<?php the_title(); ?>
</a>
</article>
<?php endwhile; ?>
</div>
</div>
<?php endif;
endforeach;
endforeach; ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment