Last active
August 29, 2015 14:04
-
-
Save mapsam/6f682e5e78f022820882 to your computer and use it in GitHub Desktop.
wordpress custom taxonomy list of terms and posts
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 | |
// using a specified custom taxonomy slug | |
// find all taxonomy terms (categories) and | |
// the associated posts | |
$tax = 'TAXONOMY-SLUG'; | |
echo '<ul>'; | |
$terms = get_terms($tax); | |
foreach($terms as $term) { | |
echo '<li>'.$term->name; | |
// echo all posts with specific term as new list | |
// using tax_query within the WP_Query loop arguments | |
$args = array( | |
'post_type' => 'CUSTOM-POST-TYPE', | |
'tax_query' => array( | |
array( | |
'taxonomy' => $tax, | |
'field' => 'slug', | |
'terms' => $term->slug | |
) | |
) | |
); | |
global $wp_query; | |
$wp_query = new WP_Query($args); | |
echo '<ul>'; | |
while ($wp_query->have_posts()) : $wp_query->the_post(); | |
$post_title = get_the_title(); | |
$post_url = get_the_permalink(); | |
echo '<li><a href="'.$post_url.'">'.$post_title.'</a></li>'; | |
endwhile; | |
echo '</ul>'; | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment