Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On my author page I currently have a list of categories the author has posted in generated by the following code:

<?php
$categories = $wpdb->get_results("
    SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug
    FROM $wpdb->posts as posts
    LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
    LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
    LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
    WHERE 1=1 AND (
        posts.post_status = 'publish' AND
        posts.post_author = $curauth->ID AND
        tax.taxonomy = 'category' )
    ORDER BY terms.name ASC
");
$x = 0;
?>
<ul class="category-list">
    <?php foreach($categories as $category) : ?>
    <?php if( $x%2 ){ ?>
    <li class="odd">
    <?php } else { ?>
    <li>
    <?php } ?>
        <a href="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>"><?php echo $category->name ?></a>
    </li>
    <?php $x++; endforeach; ?>
</ul> 

What I'm after doing is to add the number of posts within each category in the foreach loop, next to the category in parentheses.

Any idea how would I go about doing so?

Thanks!

share|improve this question

2 Answers 2

you can try a much less elegant but a working solution which is to get all of the users posts and count their categories:

$query = new WP_Query(array(
    'author' => $curauth->ID,
    'posts_per_page' => -1
    )
    );
if ($query->have_posts()){
    $u_cats = array();
    while ($query->have_posts()){
        $query->the_post();
        $terms = wp_get_object_terms( $post->ID, 'category');
        foreach ($terms as $term){
            if (is_array($u_cats[$term->term_id])){
                $u_cats[$term->term_id]['count'] = $u_cats[$term->term_id]['count'] +1;
            }else{
                $u_cats[$term->term_id]['count'] = 1;
                $u_cats[$term->term_id]['name'] = $term->name;
                $u_cats[$term->term_id]['ID'] = $term->term_id ;
            }
        }
    }
    //Now $u_cats is an array of categories each with name, ID and author post count for that category
    $x = 0;
?>
<ul class="category-list">
    <?php foreach($u_cats as $category) : 
     if( $x%2 ){ ?>
    <li class="odd">
    <?php } else { ?>
    <li>
    <?php } ?>
        <a href="<?php echo get_category_link( $category['ID'] ); ?>" title="<?php echo $category['name'];?>"><?php echo $category['name']; ?></a>(<?php echo $category['count']; ?>)
    </li>
    <?php $x++; endforeach; ?>
</ul>
<?php 

}
share|improve this answer

why not use wp_list_categories?

wp_list_categories( 'show_count=1' );
share|improve this answer
    
Hi, the reason I didn't use wp_list_categories() is I'm just after the categories the author has posted in. (Unless there's some way of passing the author as an argument to that function?) –  Probocop Jun 30 '11 at 10:34
    
I see, my bad. No, but you could get all post IDs for the author and then use update_object_term_cache, but the count field will still be for all posts, not just the current author's. –  TheDeadMedic Jun 30 '11 at 13:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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