Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save uddhabh/85cc9dc65ef0b1a97714059f750b4c84 to your computer and use it in GitHub Desktop.
Save uddhabh/85cc9dc65ef0b1a97714059f750b4c84 to your computer and use it in GitHub Desktop.
List all posts in custom post type by taxonomy
<?php
// https://wordpress.stackexchange.com/questions/66219/list-all-posts-in-custom-post-type-by-taxonomy
$custom_terms = get_terms('states');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'cities',
'tax_query' => array(
array(
'taxonomy' => 'states',
'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