Make WordPress Core

Changeset 31058


Ignore:
Timestamp:
01/06/2015 04:57:12 PM (10 years ago)
Author:
SergeyBiryukov
Message:

Skip building the query in wp_count_posts() if cached results are used.

props MikeHansenMe.
fixes #30928.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r31042 r31058  
    23522352    $cache_key = _count_posts_cache_key( $type, $perm );
    23532353
     2354    $counts = wp_cache_get( $cache_key, 'counts' );
     2355    if ( false !== $counts ) {
     2356        /** This filter is documented in wp-includes/post.php */
     2357        return apply_filters( 'wp_count_posts', $counts, $type, $perm );
     2358    }
     2359
    23542360    $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
    23552361    if ( 'readable' == $perm && is_user_logged_in() ) {
     
    23632369    $query .= ' GROUP BY post_status';
    23642370
    2365     $counts = wp_cache_get( $cache_key, 'counts' );
    2366     if ( false === $counts ) {
    2367         $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
    2368         $counts = array_fill_keys( get_post_stati(), 0 );
    2369 
    2370         foreach ( $results as $row )
    2371             $counts[ $row['post_status'] ] = $row['num_posts'];
    2372 
    2373         $counts = (object) $counts;
    2374         wp_cache_set( $cache_key, $counts, 'counts' );
    2375     }
     2371    $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
     2372    $counts = array_fill_keys( get_post_stati(), 0 );
     2373
     2374    foreach ( $results as $row ) {
     2375        $counts[ $row['post_status'] ] = $row['num_posts'];
     2376    }
     2377
     2378    $counts = (object) $counts;
     2379    wp_cache_set( $cache_key, $counts, 'counts' );
    23762380
    23772381    /**
Note: See TracChangeset for help on using the changeset viewer.