From 3274f5fee6c08af1d5065b4f50dc004d72706f30 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 6 Jan 2015 16:57:12 +0000 Subject: [PATCH] Skip building the query in wp_count_posts() if cached results are used. props MikeHansenMe. fixes #30928. git-svn-id: https://develop.svn.wordpress.org/trunk@31058 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 231741607e..c4257e4a2e 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -2351,6 +2351,12 @@ function wp_count_posts( $type = 'post', $perm = '' ) { $cache_key = _count_posts_cache_key( $type, $perm ); + $counts = wp_cache_get( $cache_key, 'counts' ); + if ( false !== $counts ) { + /** This filter is documented in wp-includes/post.php */ + return apply_filters( 'wp_count_posts', $counts, $type, $perm ); + } + $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; if ( 'readable' == $perm && is_user_logged_in() ) { $post_type_object = get_post_type_object($type); @@ -2362,18 +2368,16 @@ function wp_count_posts( $type = 'post', $perm = '' ) { } $query .= ' GROUP BY post_status'; - $counts = wp_cache_get( $cache_key, 'counts' ); - if ( false === $counts ) { - $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); - $counts = array_fill_keys( get_post_stati(), 0 ); + $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); + $counts = array_fill_keys( get_post_stati(), 0 ); - foreach ( $results as $row ) - $counts[ $row['post_status'] ] = $row['num_posts']; - - $counts = (object) $counts; - wp_cache_set( $cache_key, $counts, 'counts' ); + foreach ( $results as $row ) { + $counts[ $row['post_status'] ] = $row['num_posts']; } + $counts = (object) $counts; + wp_cache_set( $cache_key, $counts, 'counts' ); + /** * Modify returned post counts by status for the current post type. *