WordPress.org

Make WordPress Core

Ticket #39242: 39242.2.diff

File 39242.2.diff, 1.3 KB (added by desrosj, 13 months ago)
  • src/wp-includes/user.php

     
    372372
    373373        $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );
    374374
    375         $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
     375        $cache_key = "count_user_{$post_type}_{$userid}";
     376
     377        if ( $public_only ) {
     378                $cache_group = 'user_posts_count_public';
     379        } else {
     380                $cache_group = 'user_posts_count';
     381        }
     382
     383        $count = wp_cache_get( $cache_key, $cache_group );
     384
     385        if ( false === $count ) {
     386                $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
     387                wp_cache_add( $cache_key, $count, $cache_group );
     388        }
    376389
    377390        /**
    378391         * Filters the number of posts a user has written.
     
    409422                return $count;
    410423        }
    411424
    412         $userlist = implode( ',', array_map( 'absint', $users ) );
    413         $where    = get_posts_by_author_sql( $post_type, true, null, $public_only );
    414 
    415         $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
    416         foreach ( $result as $row ) {
    417                 $count[ $row[0] ] = $row[1];
     425        foreach ( $users as $user_id ) {
     426                $count[ $user_id ] = count_user_posts( $user_id, $post_type, $public_only );
    418427        }
    419428
    420429        foreach ( $users as $id ) {