Show all terms of a custom taxonomy?

  • I made a few custom taxonomies and I need to show ALL the terms from it, what I achieved so far is showing the taxonomies that are selected/chosen in a custom post type but I need all of them to show, wether it's selected or not. So that later I can make a filter that filters according to which terms a custom post type value contains.

    <?php 
    $args=array(
      'public'   => true,
      '_builtin' => false
    );
    $output = 'names'; // or objects
    $operator = 'and';
    $taxonomies=get_taxonomies($args,$output,$operator); 
    if  ($taxonomies) {
      foreach ($taxonomies  as $taxonomy ) {
        $terms = get_terms($taxonomy);
            foreach ( $terms as $term) {
    ?>
            <li><br><a href="#"><input type="checkbox" name="profile[]" value=""><?php echo $term->name; ?> <br/></a></li>
           <?php 
                    }
                  }
                }  
                ?>
    

    What I have so far.

    Thanks in advance!

    At what point does this fail? How much of it works the way you'd like?

    It works the issue is that I can only show the SELECTED terms in a custom post type. I want all of them to show wether selected or not, I don't want to have a dummy post type that has everything selected just to show them.

  • s_ha_dum

    s_ha_dum Correct answer

    7 years ago

    You need to pass an additional argument to get_terms(). The default is to hide "empty" terms-- terms which are assigned to no posts.

    $terms = get_terms([
        'taxonomy' => $taxonomy,
        'hide_empty' => false,
    ]);
    

    Thank you so much! But I want to ask something, why would you create an array inside a variable instead of declaring it above where the first array is shown?

    Simplicity. If the argument array were more complex I would have declared it first (most likely), but for a single argument that is just the most straightforward way to do it. It should work equally well either way.

    Thanks a lot :) that thought me a lot. I really appreciate it!

    Works!! Now I can see what's going on with all the taxonomy options! Some plugins create complex structure in there.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM