Created
March 4, 2022 17:20
-
-
Save diggeddy/5c7b2bfe6c7f792e697f369bf352863f to your computer and use it in GitHub Desktop.
Multiple Loop through Category Terms
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 | |
function db_multi_term_custom_loop($atts, $content = null) { | |
// Get all the categories | |
$categories = get_terms( 'category' ); | |
ob_start(); | |
// Loop through the $category terms | |
foreach ( $categories as $category ): | |
// new query for each category term. | |
$customQuery = new WP_Query( | |
array( | |
'post_type' => 'post', | |
'showposts' => -1, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'category', | |
'terms' => array( $category->slug ), | |
'field' => 'slug' | |
) | |
) | |
) | |
); | |
// output category title and posts | |
?> | |
<h2><?php echo $category->name; ?></h2> | |
<div class="gb-grid-wrapper gb-grid-wrapper-global"> | |
<?php while ($customQuery->have_posts()) : $customQuery->the_post(); ?> | |
<div class="gb-grid-column gb-grid-column-global-33"> | |
<?php do_action('db_custom_post_loop'); ?> | |
</div> | |
<?php endwhile; ?> | |
</div> | |
<?php | |
// Reset query | |
$customQuery = null; | |
wp_reset_postdata(); | |
// end the loop | |
endforeach; | |
return ob_get_clean(); | |
} | |
add_shortcode('db_multi_term_loop', 'db_multi_term_custom_loop'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment