Last active
March 23, 2020 16:12
-
-
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
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 | |
$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; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this code will show this kind of result: