Last active
November 3, 2017 01:21
-
-
Save chrdesigner/3256cf7787364eb11a87 to your computer and use it in GitHub Desktop.
Display Groups of Custom Posts by their Custom Taxonomy 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
<?php | |
$post_type = 'post'; | |
// Get all the taxonomies for this post type | |
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) ); | |
foreach( $taxonomies as $taxonomy ) : | |
// Gets every "category" (term) in this taxonomy to get the respective posts | |
$terms = get_terms( $taxonomy, | |
array( | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'hide_empty' => '1' | |
) | |
); | |
foreach( $terms as $term ) : | |
// WP_Query arguments | |
$args = array ( | |
'post_type' => $post_type, | |
'posts_per_page' => '-1', | |
'tax_query' => array( | |
array( | |
/** | |
* For get a specific taxanomy use | |
*'taxonomy' => 'category', | |
*/ | |
'taxonomy' => $taxonomy, | |
'field' => 'slug', | |
'terms' => $term->slug, | |
) | |
) | |
); | |
// The Query | |
$query = new WP_Query( $args ); | |
// The Loop | |
if( $query->have_posts() ) : ?> | |
<dl id="box-loop-list-<?php echo $term->slug ;?>"> | |
<dt><h3><?php echo $term->name ;?></h3></dt> | |
<?php while( $query->have_posts() ) : $query->the_post(); ?> | |
<dd> | |
<?php if(has_post_thumbnail()) { ?> | |
<figure><?php the_post_thumbnail('thumbnail');?></figure> | |
<?php } ?> | |
<a href="<?php get_the_permalink();?>" title="<?php the_title();?>."><?php the_title();?></a> | |
</dd> | |
<?php endwhile; ?> | |
</dl><?php | |
endif; | |
endforeach; | |
// End foreach term | |
endforeach; | |
// End foreach taxonomy | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment