0

I have a custom taxonomy lets call it "indications". We use it with a custom post type "Product". We use a hierarchy for the terms Parent->Child. The Parent has never an connection with a product. I use it to make a custom select field and would need the parents to be the optgroup header.

Is use this structure Parent

  • sub child 1 (2 posts according to backend)
  • sub child 2 (1 post according to backend)

But if I query for the child theme and check the count it always shows with NULL. In the Wordpress Dashboard it shoes it correctly with 2 and 1.

Why not in the front end.

  $args = array(
          'hide_empty' => false, 
          'orderby' => 'name'
  );
      
  $ins = get_terms('indikationen', $args);

if($ins):
    echo "<select>";
    foreach($ins as $in):

      
        if ( count( get_term_children( $in->term_id, 'indikationen' ) ) > 0 ):
        echo '<optgroup label="'.$in->name.'">';
          foreach($ins as $sub):
            if($sub->parent == $in->term_id):
                echo "<option value='".$sub->term_id."'>".$sub->name. " count: ".$sub->count."</option>";
          // SUB-COUNT is always NULL but has correct count in backend
            
            endif;
          endforeach;
        echo '</optgroup>';
        
        else:
          if($in->count):
            echo "<option value='".$in->term_id."'>" .$in->name. " (".$in->count.")</option>";
          endif;
        endif;  
    
    endforeach;
    echo "</select>";
  endif;


What am I doing wrong?

var_dump($sub);
// shows me for count also NULL

Thanks

2 Answers 2

0

So I came up with a workaround solution. Not sure if it is the fastest and best solution but it works perfectly fine for our custom field.

This works with custom post types and custom taxonomies and sorts everything A->Z.

Any better solution?

// get all posts from custom post type and their TERMS
// suppress filters is for WPML
      
// update to your custom_post_type

$posts_in_post_type = get_posts( array(
    'fields' => 'ids',
    'post_type' => 'custom_post_type',
    'posts_per_page' => -1,
    'suppress_filters' => 0
) );

// update custom_taxonomy 
$ins = wp_get_object_terms( $posts_in_post_type, 'custom_taxonomy', array( 'ids' ) ); 

// CREATE array to store the values
$cTerms = array();

  
if($ins):

  // First add all parents and also a bool to check if it is a parent or not
  foreach($ins as $in):
    if($in->parent):
      $cTerms[$in->parent]['name'] = get_term( $in->parent )->name;
      $cTerms[$in->parent]['parent'] = 1;
    endif;
  endforeach;

  // add values - check if the value has a parent or not
  // if has parent add it to [childs]
  foreach($ins as $in):
    if($in->parent):
      $newdata = array($in->term_id => $in->name, "name" => $in->name, "child_id" => $in->term_id);
      $cTerms[$in->parent]['childs'][] = $newdata;
    else:
      $cTerms[$in->term_id]['name'] = $in->name;
      $cTerms[$in->term_id]['parent'] = 0;
    endif;
  endforeach;

  // SORT Main Array
  usort($cTerms, function($a, $b) {
    return $a['name'] <=> $b['name'];
  });

  // SORT Childs
  if($cTerms):
    foreach($cTerms as $cterm):
      if($cterm['parent']):
        usort($cterm['childs'], function($a, $b) {
          return $a['name'] <=> $b['name'];
        });
      endif;
    endforeach;
  endif;


endif;

// possible output as select with parent as optgroup header
echo "<select>";
foreach($cTerms as $key => $menu):

  if($menu['parent']):
    echo '<optgroup label="'.$menu['name'].'">';      
    if($menu['childs']):
     foreach($menu['childs'] as $child):
       echo '<option value="'.$child['child_id'].'">'. $child['name'] .'</option>';
     endforeach;
     echo '</optgroup>';  
    endif;
  else:
    echo '<option value="'.$key.'">'. $menu['name'] .'</option>';
  endif;

endforeach;
echo "</select>";
0

I know this is old, but I recently found myself in this situation.

It turned out that the 'update_count_callback' on the taxonomy register was incorrect; it should be set to '_update_generic_term_count' for custom post types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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