WordPress at Your Fingertips

count_user_posts()WP 3.0.0

Gets the number of posts of a specified type for a specified user (author).

WordPress doesn't have the count_author_posts() function, but this function makes complete sense.

If you want to get the number of posts of multiple users at once, use count_many_users_posts().

1 time — 0.001945 sec (very slow) | 50000 times — 46.58 sec (very slow) | PHP 7.1.2, WP 4.7.3
Hooks from the function


String. Number of posts the user has written in this post type.


count_user_posts( $userid, $post_type, $public_only );
$userid(number) (required)
the ID of the user whose post count you want to get.
The post type, the number of items of which you want to count.
Defaults: 'post'

Whether to get only public posts - post_status = publish.

  • false - count private posts as well. In the WHERE part of the query, the condition OR post_status = 'private' will be added. I.e. private posts will be added to the query for a user who is allowed to view private posts.

  • true - only public posts will be counted, regardless of user privileges.

Default: false



#1 Demo

$count = count_user_posts( 1 ); // string(2) "16"

#2 Get the number of posts of the user

Print an inscription showing how many posts are published by user with ID 1:

<?php echo 'Total posts: ' . count_user_posts( 1 ); ?>

Here we get: Total posts: 321.


#3 Number of posts (custom type) of the author

Suppose we have a post type book and we need to count how many posts of this type have been published by a user with ID = 5:

<?php echo 'Total books published by user 5: ' . count_user_posts( 5, 'book' ); ?>

#4 Support for custom post types. For WP versions less than 4.1.

This example is not relevant for WP 4.1+ versions, because there is a second option that allows you to do this.

To be able to count the number of custom post types, you have to create your own function. Here, the post type is specified in the second parameter.

function count_user_posts_by_type( $userid, $post_type = 'post' ){
	global $wpdb;

	$where = get_posts_by_author_sql( $post_type, TRUE, $userid );

	$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

	return apply_filters( 'get_usernumposts', $count, $userid );


  • Global. wpdb. $wpdb WordPress database abstraction object.


Since 3.0.0 Introduced.
Since 4.1.0 Added $post_type argument.
Since 4.3.0 Added $public_only argument. Added the ability to pass an array of post types to $post_type.

count_user_posts() code WP 6.5.4

function count_user_posts( $userid, $post_type = 'post', $public_only = false ) {
	global $wpdb;

	$where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );

	$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

	 * Filters the number of posts a user has written.
	 * @since 2.7.0
	 * @since 4.1.0 Added `$post_type` argument.
	 * @since 4.3.1 Added `$public_only` argument.
	 * @param int          $count       The user's post count.
	 * @param int          $userid      User ID.
	 * @param string|array $post_type   Single post type or array of post types to count the number of posts for.
	 * @param bool         $public_only Whether to limit counted posts to public posts.
	return apply_filters( 'get_usernumposts', $count, $userid, $post_type, $public_only );
1 comment
    Log In