0

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!

2 Answers 2

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 

}
0

why not use wp_list_categories?

wp_list_categories( 'show_count=1' );
2
  • 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
    Commented Jun 30, 2011 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. Commented Jun 30, 2011 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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