I can’t manage to make get_terms()
to return the empty terms and I’ve tried it in a number of ways.
Here’s the code:
$terms = get_terms('device',array('hide_empty' => 0));
foreach($terms as $term) {
if($term->parent == 0) {
if($i++ != 0) echo '</optgroup>'; echo '<optgroup label="'.$term->name.'">';
$id = $term->term_id;
$args = array("child_of"=>$id);
$this_term = get_terms('device',$args);
foreach($this_term as $the_term) {
$term_name = str_replace($term->name,'',$the_term->name);
echo '<option value="'.$the_term->term_id.'">'.$the_term->name.'</option>';
}
}
}
I’ve tried all the possible ways:
$terms = get_terms('device',array('hide_empty' => false))
$terms = get_terms('device',array('hide_empty' => 0))
$terms = get_terms('device',array('hide_empty=false'))
$terms = get_terms('device',array('hide_empty=0'))
Also tried the last two ways without array
. Nothing seems to be working. It returns all the terms that have posts, but no empty one.
2 Answers
You use the hide_empty
argument for $terms
, but not for $this_term
inside your loop.
Also, with the way you’re generating your select, it would be a lot more efficient to just query top-level terms for the main loop:
$terms = get_terms( 'device', array( 'hide_empty' => false, 'parent' => 0 ) );
And then drop if($term->parent == 0) {...
inside your loop.