count_many_users_posts( int[] $users, string|string[] $post_type = 'post', bool $public_only = false ): string[]
- Since
- 3.0.0
- Source
wp-includes/user.php:618
Compatibility
- WordPress
- since 3.0.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
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
- 8–42
- Plugin surface
- None
- 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 53.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- sqldatabase query
->get_results()called directly - hookthird-party callbacks
apply_filters_deprecated()one call below count_many_users_posts()
Further down the call graph this can also reach query, option, cache, 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_many_users_posts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–10 | none |
!empty($users) && is_array($users) | 40–42 | array_map(), get_posts_by_author_sql(), ->get_results() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 53 | 8–42 | 7 | |
| 8.5 | 53 | 8–42 | 7 | |
| 8.4 | 53 | 8–42 | 7 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 57 | 8–46 | 7 | |
| 8.2 | 57 | 8–46 | 7 | |
| 8.1 | 57 | 8–46 | 7 | |
| 7.4 | 57 | 8–46 | 7 |
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.
Uses · 1
- get_posts_by_author_sql()Retrieves the post SQL based on capability, author, and type.
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; $count = array(); if ( empty( $users ) || ! is_array( $users ) ) { return $count; } $userlist = implode( ',', array_map( 'absint', $users ) ); $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); foreach ( $result as $row ) { $count[ $row[0] ] = $row[1]; } foreach ( $users as $id ) { if ( ! isset( $count[ $id ] ) ) { $count[ $id ] = 0; } } 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 6.7.7 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.