count_user_posts by user ID and by post_type

digital pro colors

You may be aware that count_user_posts has been around since about 3.0. However as of 4.1 you are able to optionally define a post_type. This is a perfect little snippet if you have a blog with multiple post types and you wanted to show specific counts for authors. I could use something like this for WordPress snippets vs tutorials sections and it would be perfect. Just add this snippet in the template you wish to display the count and you are ready to go.

Change USER to a specific ID or variable.

echo count_user_posts(USER,'post');             // posts
echo count_user_posts(USER,'page');             // pages
echo count_user_posts(USER,'CUSTOM_POST_TYPE'); // custom post types

Not to complex but a way to display a message based on the total posts a user has created.

if(count_user_posts(USER,'post') > 10)
	echo 'greater than';
	echo 'less than';

If you like this snippet you should also take a look at How to Track post views without a plugin using post meta.

Comments  Leave a Reply

  1. Thanks for this – exactly what I needed for our intercom function.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!