Skip to content

Instantly share code, notes, and snippets.

@thoronas
Created May 14, 2013 23:13
Show Gist options
  • Save thoronas/5580467 to your computer and use it in GitHub Desktop.
Save thoronas/5580467 to your computer and use it in GitHub Desktop.
Query posts from every term in a taxonomy
<?php
$terms = get_terms('taxonomy');
$count = count($terms);
//check if there are any terms
if ( $count > 0 ){
//loop through each term and query posts.
foreach($terms as $term){
$term_args = array(
'post_type' => 'post type',
'tax_query' => array(
array(
'taxonomy' => 'taxonomy',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$term_posts = new WP_Query($term_args);
if($term_posts->have_posts()):
while($term_posts->have_posts()): $term_posts->the_post();
the_title();
the_content();
endwhile; wp_reset_query();
endif;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment