1

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.

3

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>';
        }
    }
}
  • Thanks for your answere. But it's not exactly what I want. I my case, I don't know the taxonomy slug, the terms slug. My aim is to find all these information automatically without knowing everything. – freaky Apr 16 '15 at 6:40
  • I added get_object_taxonomies so now it dynamically pulls all taxonomies and terms for each post type. I also added hide_empty => false to make sure it returns unused terms as well. – mrbobbybryant Apr 19 '15 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.