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!
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.In translation ready form.
Storage in a variable.