Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Grouped taxonomy term query while excluding empty groups WordPress

  • SOLVED

Hi all,

I'm having some trouble thinking through a solution for this. I'm running a query to output products tagged within a product category and group them accordingly. This all works fine, with the only issue being that categories without products in them are still showing the group header. (See screenshot - Specialty). The issue is that this loop is actually running on a single post page (within a CPT). Both the page and the products SHARE a taxonomy -- so essentially, it is pulling in matching products (and these are then grouped into a product category) that belong to the same term as the page the user is on. Because of this, terms are not actually empty as far as wordpress is concerned (which is why hide empty has no affect on the query).

Here is the code currently in use:
https://pastebin.com/vVDGr8GJ

Answers (3)

2017-05-03

Daniele Raimondi answers:

You can move lines from 13 to 30 of your code, before line 9. Then output the h3 header only if the query has one or more results.

Something like this: https://pastebin.com/Ck3cJEY0 (code not tested)


Andrew Clemente comments:

Awesome thank you! Knew it was something that was probably pretty obvious that I was overlooking.

2017-05-03

Rempty answers:

Hello
Please take a look to this
https://dfactory.eu/get_terms-post-type/
You will can show the terms by post_type

2017-05-03

Arnav Joy answers:

Please check this code

<?php $term_listx = wp_get_post_terms($post->ID, 'odclass', array("fields" => "ids")); ?>
<?php $terms = get_terms( array(
'taxonomy' => 'odprodcat',
'hide_empty' => true,
));
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {

$loop = new WP_Query( array(
'post_type' => 'odproducts',
'orderby' => 'ID',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'odclass',
'field' => 'term_id',
'terms' => $term_listx
),
array(
'taxonomy' => 'odprodcat',
'field' => 'id',
'terms' => $term->term_id
),
)
));

if($loop->have_posts()) :
echo '<div class="groupbar">';
echo '<h3 class="resourceterm">' . $term->name . '</h3>';
echo '</div>';
echo '<div class="grouping">';

?>
<?php $i = 0; // Create a new (incrementing) var ?>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>

<?php if(has_term('mainsails', 'odprodcat' ) || has_term('jibs', 'odprodcat' ) || has_term('spinnakers', 'odprodcat' )) { ?>

<?php $i++; // Increase count ?>

<div class="contentblock sails">
<div class="photoblock"><a href="<?php the_permalink(); ?>" title="Permalink to <?php the_title(); ?>"><?php if(has_post_thumbnail()) { ?><?php the_post_thumbnail('full'); ?><?php } else { ?><img src="<?php bloginfo('stylesheet_directory'); ?>/images/ns-fade.png" alt="North Sails Logo Fade" /><?php } ?></div>
<div class="cblock">
<h2><a href="<?php the_permalink(); ?>" title="Permalink to <?php the_title(); ?> product page"><?php the_title(); ?></a></h2>
<?php if(get_field('customsubhead')) { ?><h3><?php the_field('customsubhead'); ?></h3><?php } ?>
<?php the_content(); ?>
<p><a href="<?php the_permalink(); ?>" title="Permalink to <?php the_title(); ?> product details page"><strong>Full Product Details</strong></a></p>
</div>
</div>

<?php if ($i % 2 == 0) { ?><div class="gapper"></div><?php } ?>

<?php } else { ?>

<?php $i++; // Increase count ?>

<div class="contentblock sails">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>

<?php if ($i % 2 == 0) { ?><div class="gapper"></div><?php } ?>

<?php } ?>

<?php endwhile; ?>

<div style="clear:both;"></div>

</div><!-- end grouping -->
<?php endif; ?>
<?php wp_reset_postdata(); } } ?>

<?php remove_filter('post_limits', 'your_query_limit'); ?>