Reputation: 183
I have an author page in my Wordpress blog but I also would like to show the amount of posts per author as a number. I don't know much about database queries so I would like to know how to extend the following query with the amount of posts per user.
Here's my code so far:
function contributors() {
global $wpdb;
$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name <> 'admin' ORDER BY display_name");
foreach ($authors as $author ) {
echo "<li>";
echo "<div class='author-image'>";
echo "<a href=\"".get_bloginfo('url')."/?author=";
echo $author->ID;
echo "\">";
echo userphoto($author->ID);
echo "</a>";
echo "</div>";
echo "<div class='meta'>";
echo "<h2 class='author-name'>";
the_author_meta('display_name', $author->ID);
echo "</h2>";
echo "<span class='count'>";
count_user_posts($author->ID); //Not working
echo " Articles</span>";
echo "<p class='description'>";
the_author_meta('description', $author->ID);
echo "</p>";
echo "<span class='top-dotted'></span>";
echo "</div>";
echo "</li>";
}
}
Upvotes: 2
Views: 2742
Reputation: 4397
You'll just need to add a subselect to calculate this per user.
I can achieve basically the correct result using the following query:
SELECT ID,
user_nicename,
(select count(*) from wp_posts where post_author=w.ID) as posts
FROM wp_users w
WHERE display_name <> 'admin'
ORDER BY display_name
Note the I have name the table wp_users in the query w
so we can reference the correct ID in the subselect.
Upvotes: 3