wppaste
WordPress

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
Gets the number of posts written by a list of users.

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

Reaches the database via ->get_results().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–42

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 53.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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 callbacksapply_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.

WhenInstructionsCalls it makes
always8–10none
!empty($users) && is_array($users)40–42array_map(), get_posts_by_author_sql(), ->get_results()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev538–427
8.5538–427
8.4538–4274 fewer instructions than PHP 8.3
8.3578–467
8.2578–467
8.1578–467
7.4578–467

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

Used by · 1

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Return type changed from string[] to array<int,.verified against source
3.0.0
Introduced.from the docblock

About 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.