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.

I want to list all the users of my site and the amount of posts each user has.

So far I have the function below, but I don't know how to pull in each user's ID to use in the count_user_posts...

<ul>
<?php foreach ( get_users('order=DESC&orderby=post_count') as $user ) : ?>
    <li style="color:#fff;"><span style="font-weight:bold;">
    <?php echo $user->display_name; ?></span> as <?php echo $user->user_nicename; ?> (<?php echo $user->post_count; ?> Posts)<br />
    <?php echo 'Posts made: ' . count_user_posts(1); ?>
    </li><br />
<?php endforeach; ?>
</ul>

I have foreach function going, but count_user_posts requires an integer I don't pull in each individual user ID to grab their post amount.

I would appreciate some help with this. I know it's probably something simple... I'm kind of new to php :D. Thanks a lot.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

WordPress has a wp_list_authors function with the option to list post count.

<?php
$args = array( 'orderby' => 'post_count', 'optioncount' => true);
wp_list_authors( $args );
?> 

EDIT: While it's possible to do it the way you're doing it and use count_user_posts(), this will trigger a query to retrieve users, then additional queries to get each user's count. this is a very inefficient way to do this and may have a significant impact on performance if you have a lot of authors. A better way is to select users, and count the posts all in one query. If you look in wp-includes/author-template.php you'll see the query wp_list_authors generates to do this.

share|improve this answer
    
Thanks this is exactly what I needed! –  user1893 May 3 '11 at 16:18
<?php echo 'Posts made: ' . count_user_posts($user->ID); ?>
share|improve this answer

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.