$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>get_terms only show term if there is a post using it|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

get_terms only show term if there is a post using it

matteradmin10PV0评论

I have a custom post type called user_images. I would like to create some filters and populate the dropdowns dynamically. I am using the follow code:

$post_type = 'user_images';
$taxonomies = get_object_taxonomies((object) array('post_type' => $post_type));
$terms = get_terms('image_categories');
foreach( $terms as $term ){
  echo $term->name;
}

This is correctly listing my custom taxonomy Image Categories but it is showing terms even if none of the posts have that term assigned to it. How can I only list out the terms which are associated with posts and within this specific custom post type?

I have a custom post type called user_images. I would like to create some filters and populate the dropdowns dynamically. I am using the follow code:

$post_type = 'user_images';
$taxonomies = get_object_taxonomies((object) array('post_type' => $post_type));
$terms = get_terms('image_categories');
foreach( $terms as $term ){
  echo $term->name;
}

This is correctly listing my custom taxonomy Image Categories but it is showing terms even if none of the posts have that term assigned to it. How can I only list out the terms which are associated with posts and within this specific custom post type?

Share Improve this question asked Jan 16, 2019 at 16:49 user13286user13286 2272 gold badges13 silver badges29 bronze badges 1
  • Maybe you can look at object_ids and hide_empty of get_terms()? I think there was a similar question here recently but can't seem to find it. – birgire Commented Jan 16, 2019 at 16:57
Add a comment  | 

1 Answer 1

Reset to default 4

get_terms should hide empty terms by default, but you can force it to setting hide_empty argument to true:

$terms = get_terms( array(
    'taxonomy' => 'image_categories',
    'hide_empty' => false,
) );
Post a comment

comment list (0)

  1. No comments so far