Skip to content

Instantly share code, notes, and snippets.

@dospuntocero
Last active March 23, 2020 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dospuntocero/ad601fca3f777967619e826dea8b6bed to your computer and use it in GitHub Desktop.
Save dospuntocero/ad601fca3f777967619e826dea8b6bed to your computer and use it in GitHub Desktop.
looping through a custom post type and its taxonomy. showing results by taxonomy term
<?php
$custom_post_type = 'team_profiles;
$tax_term = 'department';
$terms = get_terms($tax_term);
foreach($terms as $term) :
?>
<h2><?= $term->name;?></h2>
<?php
$cat_posts_args = array(
'post_type' => $custom_post_type,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $tax_term,
'field' => 'id',
'terms' => $term->term_id,
'include_children' => false
)
)
);
$cat_posts = new WP_Query( $cat_posts_args );
if ( $cat_posts->have_posts() ) :
?>
<ul>
<?php
while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?>
<li>
<?php the_title();?>
</li>
<?php endwhile;?>
</ul>
<?php endif;wp_reset_postdata();?>
<?php endforeach; ?>
@dospuntocero
Copy link
Author

dospuntocero commented Jan 7, 2018

this code will show this kind of result:

  • CAT A
    • Item 1a
  • CAT B
    • Item 2a
    • Item 2b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment