@@ -649,13 +649,14 @@ function count_user_posts( $userid, $post_type = 'post', $public_only = false )
649649 * Gets the number of posts written by a list of users.
650650 *
651651 * @since 3.0.0
652+ * @since 6.9.0 The results are now cached.
652653 *
653654 * @global wpdb $wpdb WordPress database abstraction object.
654655 *
655656 * @param int[] $users Array of user IDs.
656657 * @param string|string[] $post_type Optional. Single post type or array of post types to check. Defaults to 'post'.
657658 * @param bool $public_only Optional. Only return counts for public posts. Defaults to false.
658- * @return string[] Amount of posts each user has written, as strings, keyed by user ID.
659+ * @return array<int, string> Amount of posts each user has written, as strings, keyed by user ID.
659660 */
660661function count_many_users_posts ( $ users , $ post_type = 'post ' , $ public_only = false ) {
661662 global $ wpdb ;
@@ -682,14 +683,31 @@ function count_many_users_posts( $users, $post_type = 'post', $public_only = fal
682683 return $ pre ;
683684 }
684685
685- $ userlist = implode ( ', ' , array_map ( 'absint ' , $ users ) );
686- $ where = get_posts_by_author_sql ( $ post_type , true , null , $ public_only );
686+ // Cleanup the users array. Remove duplicates and sort for consistent ordering.
687+ $ users = array_unique ( array_filter ( array_map ( 'intval ' , $ users ) ) );
688+ sort ( $ users );
687689
688- $ result = $ wpdb ->get_results ( "SELECT post_author, COUNT(*) FROM $ wpdb ->posts $ where AND post_author IN ( $ userlist) GROUP BY post_author " , ARRAY_N );
690+ // Cleanup the post type argument. Remove duplicates and sort for consistent ordering.
691+ $ post_type = array_unique ( (array ) $ post_type );
692+ sort ( $ post_type );
693+
694+ $ userlist = implode ( ', ' , $ users );
695+ $ where = get_posts_by_author_sql ( $ post_type , true , null , $ public_only );
696+ $ query = "SELECT post_author, COUNT(*) FROM $ wpdb ->posts $ where AND post_author IN ( $ userlist) GROUP BY post_author " ;
697+ $ cache_key = 'count_many_users_posts: ' . md5 ( $ query );
698+ $ cache_salts = array ( wp_cache_get_last_changed ( 'posts ' ), wp_cache_get_last_changed ( 'users ' ) );
699+ $ count = wp_cache_get_salted ( $ cache_key , 'post-queries ' , $ cache_salts );
700+
701+ if ( false === $ count ) {
702+ $ where = get_posts_by_author_sql ( $ post_type , true , null , $ public_only );
703+ $ result = $ wpdb ->get_results ( "SELECT post_author, COUNT(*) FROM $ wpdb ->posts $ where AND post_author IN ( $ userlist) GROUP BY post_author " , ARRAY_N );
704+
705+ $ count = array_fill_keys ( $ users , 0 );
706+ foreach ( $ result as $ row ) {
707+ $ count [ $ row [0 ] ] = $ row [1 ];
708+ }
689709
690- $ count = array_fill_keys ( $ users , 0 );
691- foreach ( $ result as $ row ) {
692- $ count [ $ row [0 ] ] = $ row [1 ];
710+ wp_cache_set_salted ( $ cache_key , $ count , 'post-queries ' , $ cache_salts , HOUR_IN_SECONDS );
693711 }
694712
695713 return $ count ;
0 commit comments