Make WordPress Core

Ticket #39242: 39242.diff

File 39242.diff, 1.4 KB (added by milindmore22, 7 years ago)

Cache for count_user_posts

  • src/wp-includes/user.php

    diff --git src/wp-includes/user.php src/wp-includes/user.php
    index 9a685b9b36..82ad168063 100644
    function count_user_posts( $userid, $post_type = 'post', $public_only = false ) 
    381381
    382382        $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );
    383383
    384         $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
     384        $cache_key = "count_user_posts_$userid";
     385        $count     = wp_cache_get( $cache_key, 'user_posts_count' );
     386
     387        if ( false === $count ) {
     388                $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
     389                wp_cache_add( $cache_key, $count, 'user_posts_count', 5 * MINUTE_IN_SECONDS );
     390        }
    385391
    386392        /**
    387393         * Filters the number of posts a user has written.
    function count_many_users_posts( $users, $post_type = 'post', $public_only = fal 
    418424                return $count;
    419425        }
    420426
    421         $userlist = implode( ',', array_map( 'absint', $users ) );
    422         $where    = get_posts_by_author_sql( $post_type, true, null, $public_only );
    423 
    424         $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
    425         foreach ( $result as $row ) {
    426                 $count[ $row[0] ] = $row[1];
     427        foreach ( $users as $user_id ) {
     428                $count[ $user_id ] = count_user_posts( $user_id, $post_type, $public_only );
    427429        }
    428430
    429431        foreach ( $users as $id ) {