count_user_posts( int $userid, array|string $post_type = 'post', bool $public_only = false ): string
- Since
- 3.0.0, 4.1.0, 4.3.0
- Source
wp-includes/user.php:616
Compatibility
- WordPress
- since 4.3.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$useridint- User ID.
$post_typearray|stringoptional- Single post type or array of post types to count the number of posts for. Default 'post'.Default:
'post' $public_onlybooloptional- Whether to only return counts for public posts. Default false.Default:
false
Return value
string- Number of posts the user has written in this post type.
Performance profile
How much work a call to count_user_posts() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 48–58
- Plugin surface
- 1 hook
- Called by
- 4
Reaches the database via ->get_var().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 58.
Third-party callbacks on 'get_usernumposts' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get_last_changed()called directly - hookthird-party callbacks
apply_filters()called directly - sqldatabase query
->get_var()called directly
Further down the call graph this can also reach query, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost count_user_posts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$count !== false | 48 | array_unique(), sort(), get_posts_by_author_sql(), wp_cache_get_last_changed(), md5(), wp_cache_get_salted(), apply_filters() |
$count === false | 58 | array_unique(), sort(), get_posts_by_author_sql(), wp_cache_get_last_changed(), md5(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 58 instructions, 48–58 executed per call, 1 branch. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while count_user_posts() runs, in this order:
- apply_filters( get_usernumposts )filterline 645 (+29 into the body)
Filters the number of posts a user has written.
Uses · 5
- get_posts_by_author_sql()Retrieves the post SQL based on capability, author, and type.
- wp_cache_get_last_changed()Gets last changed date for the specified cache group.
- wp_cache_get_salted()Retrieves cached data if valid and unchanged.
- wp_cache_set_salted()Stores salted data in the cache.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 4
- WP_REST_Users_Controller::get_item_permissions_check()Checks if a given request has access to read a user.
- get_the_author_posts()Retrieves the number of posts by the author of the current post.
- get_usernumposts()Retrieves the number of posts a user has written.
- twentyfourteen_list_authors()Prints a list of all site contributors who published at least one post.
Source code
function count_user_posts( $userid, $post_type = 'post', $public_only = false ) { global $wpdb; $post_type = array_unique( (array) $post_type ); sort( $post_type ); $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only ); $query = "SELECT COUNT(*) FROM $wpdb->posts $where"; $last_changed = wp_cache_get_last_changed( 'posts' ); $cache_key = 'count_user_posts:' . md5( $query ); $count = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed ); if ( false === $count ) { $count = $wpdb->get_var( $query ); wp_cache_set_salted( $cache_key, $count, 'post-queries', $last_changed ); } /** * 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 string $count The user's post count as a numeric string. * @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 );}Changelog
Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$public_only argument. Added the ability to pass an array of post types to $post_type.from the docblock$post_type argument.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.