Skip to content

Instantly share code, notes, and snippets.

@thetwopct
Created December 22, 2018 05:50
Show Gist options
  • Save thetwopct/435667679862408fe32e8558e46d71dc to your computer and use it in GitHub Desktop.
Save thetwopct/435667679862408fe32e8558e46d71dc to your computer and use it in GitHub Desktop.
List all posts by custom taxonomy
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
endwhile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment