Home / author / count_user_posts by user ID and by post_type
  • How to count_user_posts by user ID and by post_type

    posts, pages, snippet screen shot

    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.

    ( click code to copy )

    wordpress snippet : PHP

    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.

    ( click code to copy )

    wordpress snippet : PHP

    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.

    ( WordPress codex functions, hooks, in this snippet. )

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