[wp-trac] [WordPress Trac] #63045: Add caching to `count_many_users_posts()`
WordPress Trac
noreply at wordpress.org
Thu Aug 28 12:38:23 UTC 2025
#63045: Add caching to `count_many_users_posts()`
-------------------------------------------------+-------------------------
Reporter: flixos90 | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: 6.9
Component: Users | Version:
Severity: normal | Resolution:
Keywords: has-patch has-unit-tests 2nd- | Focuses:
opinion dev-feedback needs-refresh | performance
-------------------------------------------------+-------------------------
Changes (by kalpeshh):
* keywords: has-patch has-unit-tests 2nd-opinion dev-feedback => has-patch
has-unit-tests 2nd-opinion dev-feedback needs-refresh
Comment:
I checked the PR https://github.com/WordPress/wordpress-
develop/pull/9106/files and [attachment:63045-cache-
count_many_users_posts.diff]
The issue with both approaches is different `cache_key` would be generated
if `order of array` **$users** is different
{{{
$userlist = implode( ',', array_map( 'absint', $users ) );
$cache_key = "count_many_users_posts_{$post_type}_{$public_only}_" .
str_replace( ',', '_', $userlist ) . '_' . get_current_user_id();
}}}
For above code,
{{{
$users = [ 1, 2, 3 ] → $userlist = "1,2,3" → _1_2_3_ in the cache key.
$users = [ 3, 2, 1 ] → $userlist = "3,2,1" → _3_2_1_ in the cache key.
}}}
To fix this, we should normalize the `$users` array before building the
key:
- Ensure all IDs are integers.
- Remove duplicates.
- Sort the array to guarantee consistent order.
Here is the suggested fix for this,
{{{
$users = array_map( 'absint', (array) $users );
$users = array_filter( $users ); // remove 0/invalids
$users = array_unique( $users ); // remove duplicates
sort( $users ); // enforce consistent order
$userlist = implode( '_', $users );
$cache_key =
"count_many_users_posts_{$post_type}_{$public_only}_{$userlist}_" .
get_current_user_id();
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/63045#comment:12>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list