Nested Loops get_terms() WP_query
-
Hi there,
First off, I love your plugin!
I’m trying to use your plugin in conjunction with WooCommerce to create ‘category’ pages for products, because I’m not a massive fan of how WooCommerce handles category pages.
What I want to accomplish:
- Show ‘cptonomies’ in descending order based on how many WooCommerce ‘products’ are attached to the cptonomy: (I’ll call this: ‘mycptonomy’)
- Use a separate custom taxonomy to filter and show specifc kinds of my cptonomy (I’ll call this: ‘mytaxo’)
- Show specific custom fields for my ‘cptonomy’ (treating it like a post), within the ‘loop’ mentioned in the first point above
What I have:
- A nested loop, using get_terms() as the ‘outer loop’ and WP_Query() as the ‘inner loop’
- The ‘outer loop’ to get the a list of ‘cptonomy,’ and ordering it by ‘orderby’=> ‘count’. This was the only way I could think to order things by attached posts and this seems to work ok.
- The ‘inner’ loop’ to get specific post meta data for my cptonomy. I’m setting the slug using the outer loop and then trying to pull specific post data using that slug (since they are, effectively, the same), but I’m running into an issue using tax_query within my nested loop (it seems to have no effect). When I pull this loop outside the get_terms() loop within which I nested this loop, it works as expected.
My code:
<?php // mycptonomy Taxonomy Loop Arguments $mycptonomy_args = array( 'taxonomy' => 'mycptonomy', 'hide_empty' => false, 'orderby' => 'count', 'order' => 'DESC', 'suppress_filters' => false, ); // mycptonomy Taxonomy Loop $terms = get_terms('mycptonomy',$mycptonomy_args); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { $count = count( $terms ); $i = 0; foreach ( $terms as $term ) { // Start for each $i++; // mycptonomy Taxonomy Loop Variables $mycptonomy_count = $term->count; $mycptonomy_name = $term->name; $mycptonomy_slug = $term->slug; $mycptonomy_id = $term->term_id; ?> <?php //mycptonomy Posts WP_Query Arguments $dest_post_args = array ( 'p' => $mycptonomy_id, 'post_type' => array( 'mycptonomy' ), 'nopaging' => true, 'posts_per_page' => 1, 'tax_query' => array( // This is what isn't working array( 'taxonomy' => 'mytaxo', 'field' => 'slug', 'terms' => array('term-1','term-2','term-3'), 'operator' => 'NOT IN', ), ), ); //mycptonomy Posts WP_Query Query $query = new WP_Query($dest_post_args); //mycptonomy Posts WP_Query Loop if ( $query->have_posts() ) { $i = 0; while ( $query->have_posts() ) { $query->the_post(); $i++; $mycptonomy_post_id = get_the_ID(); $mycptonomy_post_title = get_the_title(); ?> <div class="col-md-6"> <?php echo $dest_post_id;?> - <?php echo $dest_post_title;?> (<?php echo($mycptonomy_count);?>) </div> <? } }; /* Restore original Post Data */ wp_reset_postdata(); } // End for each } ?>
My questions:
- How can I order cptonomies using get_terms() using ‘orderby’ only for a specific post type (e.g. only count ‘products’ but not other post types that may be attached to the cptonomy)
- How can I nest WP_Query within get_terms for a cptonomy and use tax_query to only show specific items for a separate custom taxonomy
Any insights would be hugely appreciated and thanks in advance!
- The topic ‘Nested Loops get_terms() WP_query’ is closed to new replies.