Created
October 23, 2022 08:55
-
-
Save uddhabh/85cc9dc65ef0b1a97714059f750b4c84 to your computer and use it in GitHub Desktop.
List all posts in custom post type by taxonomy
This file contains hidden or 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 | |
// 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