I would like to get all the available taxonomy for each (custom) post type.
My aim is to list for each post type, each taxonomy like category, tag and for each of these taxonomy to get all register terms (and ids) with the number of items for each terms,…

I would like to output something like that:

post
   Categories
      cat1 (2)
      cat2 (1)
   Tags
      Tag1 (5)
      Tag2 (3)
portfolio
   Attributes
      att1 (2)
      att2 (1)
      att3 (5)
      att4 (9)
   Categories
      cat1 (2)
      cat2 (4)
      cat2 (1)
   Tags
      Tag1 (5)
      Tag2 (3)
....

I don’t now what is the right and best way to do that. It’s a little bit more complex than I expected and I didn’t find a solution.

1 Answer
1

I would suggest querying a list of all post types using get_post_types. With that array you can do a foreach. and for each post type, query all terms with get_term. It would look something like this, but you should pass get_post_types your know post types, because as it stands, this will also display things like attachments and nav menus.

function agency_wp_test() {
    $post_types = get_post_types(); 

    foreach ( $post_types as $post_type ) {
        $taxonomy_names = get_object_taxonomies( $post_type );

        $terms = get_terms( $taxonomy_names, array( 'hide_empty' => false ));
         if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
             echo '<h5>'.$post_type.'</h5>';
             echo '<ul>';
             foreach ( $terms as $term ) {
               echo '<li>' . $term->name . '</li>';

            }
            echo '</ul>';
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *