php - Exclude wp categories without posts assigned
Get the solution ↓↓↓I have a custom post type 'consultants' and custom taxonomy 'consultants_category'. I need to output all custom taxonomies (categories) that have posts assigned. But code above output all taxonomies including those that don't have posts assigned - https://prnt.sc/zr96jh Please help me to improve this code. Thanks.
<div class="cp_allconsultants-l1_list_new">
<?php
$terms = get_terms(array(
'term' => 'consultants_category',
));
foreach( $terms as $term ):
?>
<div class="l2posts_by_cat">
<h3><?php echo $term->name; ?></h3>
<ul>
<?php
$posts = get_posts(array(
'post_type' => 'consultants',
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
'nopaging' => true,
));
foreach($posts as $post):
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
Answer
Solution:
have you tried adding
'hide_empty' => true,
to get_terms?
$terms = get_terms(array(
'term' => 'consultants_category',
'hide_empty' => true,
));
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: target class [commandmakecommand] does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.