1

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.

2 Answers 2

2

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.

0
1
<?php echo 'Posts made: ' . count_user_posts($user->ID); ?>

Your Answer

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

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