1

I am trying to get all categories which are having products but getting also the categories which are having no products.

WordPress version 4.6.1

wp_dropdown_categories( 
   array( 
          'class' => 'product-category-field',
          'id' => 'product-category',
          'name' => 'category',
          'taxonomy' => 'product_cat',
          'selected' => get_query_var('product_cat' ), 
          'hierarchical' => 1, 
          'hide_empty' => 1, 
          'value_field' => 'slug', 
          'show_count' => 1 
      )
);

Even get_terms is displaying empty categories with the below code.

<?php  $terms = get_terms('product_cat', array( 'parent' => 0 ));
     if( $terms ): 
     $original_query = $wp_query;
     foreach ( $terms as $key => $term ):
        ?>
        <li>
          <?php echo $term->name; ?>
          <ul>
          <?php
          $child_terms = get_terms(
              'product_cat',
               array(
                   'child_of' => $term->term_id,
                   'hide_empty' => true
               )
          );
          foreach ( $child_terms as $child_term  ) {
             $re_child_terms = get_terms(
                 'product_cat',
                 array(
                     'child_of' => $child_term->term_id,
                     'hide_empty' => true
                 )
             );
             if ( ! $re_child_terms ){
             ?>
         <li>
            <?php echo $child_term->name; ?>
        </li>
        <?php
        }
     }
     ?>
     </ul>
  </li>
<?php
endforeach;
$wp_query = null;
$wp_query = $original_query;
?>
</ul>
<?php endif; ?>

Note: In both case do not want to display categories having zero products.

  • Are you using any e-commerce plugin or this is just custom code? – Chittaranjan Oct 13 '16 at 6:29
  • @Chittaranjan yes I am using woocommerce but still it should behave as usual wordpress post for these two function and also working correctly in localhost . – Prafulla Kumar Sahu Oct 13 '16 at 6:31
  • @Chittaranjan for wp_dropdown_categories() , we can use wc_dropdown_categories() and it will be done, but still no success with get_terms() . – Prafulla Kumar Sahu Oct 14 '16 at 6:28
  • @Chittaranjan Solved, but searching why hide_empty was not excluding empty product categories. – Prafulla Kumar Sahu Oct 14 '16 at 7:25
0

This is very simple to list all the categories, even if they do not have any post associated with those.

You just need to set following property to false in wp_dropdown_categories. method.

'hide_empty' => 0,
'hide_if_empty' => 1

Hope this will solve your problem.

  • 'hide_empty' => 0 will allow categories with no products, what I am getting, I want to exclude categories having no products in both the cases, please read the note . – Prafulla Kumar Sahu Oct 13 '16 at 10:27
  • Try the updated code, might be it will work. – Kapil Yadav Oct 13 '16 at 10:28
  • 'hide_if_empty' (bool) True to skip generating markup if no categories are found. Default false (create select element even if no categories are found). how it will restrict categories having zero products ?? – Prafulla Kumar Sahu Oct 13 '16 at 10:31
  • Any way, I still tried your suggestion , but did not helped . :( – Prafulla Kumar Sahu Oct 13 '16 at 10:32
  • I am sure, It's kind of conflict or other problem. I have checked on fresh installation, and it works flawlessly. – Kapil Yadav Oct 13 '16 at 10:44
0

wc_product_dropdown_categories() will do the trick in place of wp_dropdown_categories() .

For get_terms part

by replacing

$re_child_terms = get_terms( 'product_cat', array( 'child_of' => $child_term->term_id, 'hide_empty' => true ) );
if ( ! $re_child_terms ){

with

   $re_child_terms = get_term_children( $child_term->term_id, 'product_cat' );
   if ( $child_term->count > 0 && empty( $re_child_terms ) ){

solved my problem.

Not sure but I think the problem with get_terms is here

// Make sure we show empty categories that have children.
        if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {
            foreach ( $terms as $k => $term ) {
                if ( ! $term->count ) {
                    $children = get_term_children( $term->term_id, $term->taxonomy );
                    if ( is_array( $children ) ) {
                        foreach ( $children as $child_id ) {
                            $child = get_term( $child_id, $term->taxonomy );
                            if ( $child->count ) {
                                continue 2;
                            }
                        }
                    }

                    // It really is empty.
                    unset( $terms[ $k ] );
                }
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.