Last active
December 2, 2023 18:43
-
-
Save softiconic/0082ece28e0cfee85b74bf2f14e02708 to your computer and use it in GitHub Desktop.
Loop through custom posts filtered by custom taxonomies in WordPress.
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 | |
// Get all the categories | |
$categories = get_terms( 'sccategory' ); | |
// Loop through all the returned terms | |
foreach ( $categories as $category ): | |
// set up a new query for each category, pulling in related posts. | |
$services = new WP_Query( | |
array( | |
'post_type' => 'scpost', | |
'showposts' => -1, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'sccategory', | |
'terms' => array( $category->slug ), | |
'field' => 'slug' | |
) | |
) | |
) | |
); | |
?> | |
<h3><?php echo $category->name; ?></h3> | |
<ul> | |
<?php while ($services->have_posts()) : $services->the_post(); ?> | |
<li><?php the_title(); ?></li> | |
<?php endwhile; ?> | |
</ul> | |
<?php | |
// Reset things, for good measure | |
$services = null; | |
wp_reset_postdata(); | |
// end the loop | |
endforeach; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment