Skip to content

Instantly share code, notes, and snippets.

@mapsam
Last active August 29, 2015 14:04
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 mapsam/6f682e5e78f022820882 to your computer and use it in GitHub Desktop.
Save mapsam/6f682e5e78f022820882 to your computer and use it in GitHub Desktop.
wordpress custom taxonomy list of terms and posts
<?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