wppaste
WordPress

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

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

Reaches the database via ->get_results().

Scaling
Scales with input

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

Instructions
7–100

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

Plugin surface
1 hook

Third-party callbacks on 'pre_count_many_users_posts' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
always7–9none
!empty($users) && is_array($users) && $pre !== null19apply_filters()
!empty($users) && is_array($users) && $pre === null && $count !== false78apply_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 === false99–100apply_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

PHPCompiledExecutedBranchesNotes
8.6-dev1077–1006
8.51077–1006
8.41077–10064 fewer instructions than PHP 8.3
8.31117–1046
8.21117–1046
8.11117–1046
7.41117–1046

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:

  1. apply_filters( pre_count_many_users_posts )filterline 681 (+20 into the body)

    Filters whether to short-circuit performing the post counts.

Uses · 5

Used by · 1

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.

  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
6.9.0
The results are now cached.from the docblock
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.