count_many_users_posts( int[] $users, string|string[] $post_type = 'post', bool $public_only = false ): array<int,
- Since
- 3.0.0, 6.9.0
- Source
wp-includes/user.php:661
Compatibility
- WordPress
- since 6.9.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
$usersint[]- Array of user IDs.
$post_typestring|string[]optional- Single post type or array of post types to check. Defaults to 'post'.Default:
'post' $public_onlybooloptional- Only return counts for public posts. Defaults to false.Default:
false
Return value
array<int,- string> Amount of posts each user has written, as strings, keyed by user ID.
Performance profile
How much work a call to count_many_users_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
- Scales with input
- Instructions
- 7–100
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via ->get_results().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 107.
Third-party callbacks on 'pre_count_many_users_posts' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get_last_changed()called directly - sqldatabase query
->get_results()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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost count_many_users_posts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–9 | none |
!empty($users) && is_array($users) && $pre !== null | 19 | apply_filters() |
!empty($users) && is_array($users) && $pre === null && $count !== false | 78 | apply_filters(), array_map(), array_filter(), array_unique(), sort(), array_unique(), sort(), get_posts_by_author_sql(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted() |
!empty($users) && is_array($users) && $pre === null && $count === false | 99–100 | apply_filters(), array_map(), array_filter(), array_unique(), sort(), array_unique(), sort(), get_posts_by_author_sql(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_results(), array_fill_keys(), wp_cache_set_salted() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 107 | 7–100 | 6 | |
| 8.5 | 107 | 7–100 | 6 | |
| 8.4 | 107 | 7–100 | 6 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 111 | 7–104 | 6 | |
| 8.2 | 111 | 7–104 | 6 | |
| 8.1 | 111 | 7–104 | 6 | |
| 7.4 | 111 | 7–104 | 6 |
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_many_users_posts() runs, in this order:
- apply_filters( pre_count_many_users_posts )filterline 681 (+20 into the body)
Filters whether to short-circuit performing the post counts.
Uses · 5
- apply_filters()Calls the callback functions that have been added to a filter hook.
- 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.
Used by · 1
- WP_Users_List_Table::display_rows()Generates the list table rows.
Source code
function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) { global $wpdb; if ( empty( $users ) || ! is_array( $users ) ) { return array(); } /** * Filters whether to short-circuit performing the post counts. * * When filtering, return an array of posts counts as strings, keyed * by the user ID. * * @since 6.8.0 * * @param string[]|null $count The post counts. Return a non-null value to short-circuit. * @param int[] $users Array of user IDs. * @param string|string[] $post_type Single post type or array of post types to check. * @param bool $public_only Whether to only return counts for public posts. */ $pre = apply_filters( 'pre_count_many_users_posts', null, $users, $post_type, $public_only ); if ( null !== $pre ) { return $pre; } // Cleanup the users array. Remove duplicates and sort for consistent ordering. $users = array_unique( array_filter( array_map( 'intval', $users ) ) ); sort( $users ); // Cleanup the post type argument. Remove duplicates and sort for consistent ordering. $post_type = array_unique( (array) $post_type ); sort( $post_type ); $userlist = implode( ',', $users ); $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); $query = "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author"; $cache_key = 'count_many_users_posts:' . md5( $query ); $cache_salts = array( wp_cache_get_last_changed( 'posts' ), wp_cache_get_last_changed( 'users' ) ); $count = wp_cache_get_salted( $cache_key, 'post-queries', $cache_salts ); if ( false === $count ) { $result = $wpdb->get_results( $query, ARRAY_N ); $count = array_fill_keys( $users, 0 ); foreach ( $result as $row ) { $count[ $row[0] ] = $row[1]; } wp_cache_set_salted( $cache_key, $count, 'post-queries', $cache_salts, HOUR_IN_SECONDS ); } return $count;}Changelog
Introduced in 3.0.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
string[] to array<int,.verified against sourceAbout 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.