Search code examples
wordpresshidecategoriescustom-post-typetaxonomy

How to hide empty with get_object_taxonomies?


Is it possible to hide taxonomies without categories. I'm using get_object_taxonomies to show all my taxonomies inside my custom post type.

<?php
            
        $blog_taxonomies = get_object_taxonomies( 'blog', 'textdomain',
        (array(
            'hide_empty' => true,
        ))
    );
        
        foreach ($blog_taxonomies as $blog_taxonomy) : 
        
        ?>

            <ul class="blog__categories-list">

                <li class="blog__categories-title"> <?= $blog_taxonomy->labels->name; ?> </li>

                <?php endforeach; ?>

Solution

  • You can use the code below. The parameters you submitted to get_object_taxonomies are incorrect.

    $taxonomy_objects = get_object_taxonomies('blog', 'objects');
            foreach ($taxonomy_objects as $taxonomy) {
                $taxonomy_terms = get_terms(['taxonomy' => $taxonomy->name, 'hide_empty' => true]);
                if (!empty($taxonomy_terms)) {
                    return;
                }
                // Your operation
            }
    

    See get_object_taxonomies documentation.