0

I have a problem (of course) and I'll go straight to the point: I need to list all the custom categories and count the posts for each category eg.

  • Category 1 (xx posts)
  • Category 2 (yy posts)
  • etc..

I tried a couple of queries but with no luck. All these categories are stored in the wp_terms table, how do I list them since I've seen that there isn't, apparently, any elations between wp_terms and the posts?
Worthless to say that i've tried wp_list_categories but it shows just "Uncategorized" since the website I'm using, uses custom post rather than "default" categores. Thanks in advance!

1
  • Erm... Every single term/post_tag/category object has a count built right into it. Have you ever tried doing a var_dump() of the object? – kaiser Jun 11 '13 at 23:59
2

After struggling a little bit, I found a solution writing this:

<?php


$cat_args = array('orderby' => 'name', 'show_count' => '1', 'hierarchical' => '0','taxonomy' => 'here goes the taxonomy');?>
<ul>
<?php
        $cat_args['title_li'] = '';
        wp_list_categories(apply_filters('', $cat_args));
?>
        </ul>
0

Something like this should work (I have tested it in a custom template file).

<?php $terms = get_terms("name_of_your_custom_taxonomy");

foreach($terms as $term)
{

echo $term->slug; // you can display term name, description ..... Look at codex to see the parameters


$items = get_posts( array(
    'post_type'   => 'name_of_your_custom_post_type)',
    'numberposts' => -1,
    'taxonomy'    => 'name_of_your_custom_taxonomy',
    'term'        => $term->slug
    ) );


$count =  count( $items );
echo $count;
}

?>
1
  • thanks for this, it gives me an output (finally) but the results is the same no matter if I change the taxonomy, eg. if i change it from hotels to restaurants, the result is still "49".Also, i need that to list all categories/custom_taxonomies not only one – Simone Jun 12 '13 at 15:21

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.