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 need to display something of a mini author statistic in my sidebar on the single posts page. I'm using the PHP code widget, to allow me to insert PHP code into a widget. What I've done is run a loop in the sidebar like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <ul>
        <li>


<?php echo 'Posts made: ' . get_usernumposts(4); ?>

<p>
Author Rank: <?php the_author_meta('user_level'); ?>
<br>
Author's Name: <?php the_author_meta('display_name'); ?>
<br>
User ID: <?php the_author_meta('ID'); ?>
</p>


        </li>

    <?php endwhile; else: ?>
        <p><?php _e('No posts by this author.'); ?></p>

    <?php endif; ?>

So that shows the number of user posts for the author ID '4'. And yes, the function still works properly though it's deprecated.

I obviously need something a bit more dynamic, as the author data varies post to post, so a static ID is not going to do the trick. I've no idea why there isn't something like

<?php the_author_meta('post_count'); ?>

I suppose that would be too convenient. I've seen a lot of queries that select post counts in various ways, all of which either aren't what I need or do not work in or outside of my loop in the sidebar.

My first thought, though I am no expert with PHP is to assign a variable to the value of usernumposts that pulls the data from the value of

<?php the_author_meta('ID'); ?>

I don't know if this is possible at all, but it would certainly achieve my goals. So can this be done? How? And if not - what are my other options?

Thanks!

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

To get the number of posts published by a user i found the solution was to call count_user_posts(), passing in the appropriate user id.

<?php echo 'Posts made: ' . count_user_posts( get_the_author_meta('ID') ); ?>

In translation ready form.

<?php printf( 'Posts made: %d', count_user_posts( get_the_author_meta('ID') ) ); ?>

Storage in a variable.

<?php $user_post_count = count_user_posts( get_the_author_meta('ID') ); ?>
share|improve this answer
2  
Codex says function is deprecated in favor of count_user_posts(). –  Rarst Feb 5 '11 at 0:09
    
Hey thanks - I was trying to find which function took its place. I appreciate that! –  Lynne Feb 5 '11 at 3:48
1  
get_usernumposts will actually call count_user_posts anyway, the only difference should be that calling the former produces a deprecated message. –  t31os Feb 5 '11 at 14:10
    
Updated the answer to use the newer function and extended the examples given. –  t31os Apr 24 '13 at 12:21
add comment

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.