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