On this page
count_user_posts( int $userid, array|string $post_type = 'post', bool $public_only = false ): string
Gets the number of posts a user has written.
Parameters
$useridint Required-
User ID.
$post_typearray|string Optional-
Single post type or array of post types to count the number of posts for. Default
'post'.Default:
'post' $public_onlybool Optional-
Whether to only return counts for public posts.
Default:
false
Return
string Number of posts the user has written in this post type.
Source
File: wp-includes/user.php. View all references
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 );
}
Hooks
- apply_filters( 'get_usernumposts',
int $count ,int $userid ,string|array $post_type ,bool $public_only ) -
Filters the number of posts a user has written.
Related
Uses
| Uses | Description |
|---|---|
| get_posts_by_author_sql() wp-includes/post.php | Retrieves the post SQL based on capability, author, and type. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| wpdb::get_var() wp-includes/class-wpdb.php | Retrieves one variable from the database. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Users_Controller::get_item_permissions_check() wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php | Checks if a given request has access to read a user. |
| get_usernumposts() wp-includes/deprecated.php | Retrieves the number of posts a user has written. |
| get_the_author_posts() wp-includes/author-template.php | Retrieves the number of posts by the author of the current post. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/count_user_posts