diff --git a/wp-includes/post.php b/wp-includes/post.php index 509f63b9ab..aed2ee42f2 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -3090,14 +3090,24 @@ function wp_count_posts( $type = 'post', $perm = '' ) { function wp_count_attachments( $mime_type = '' ) { global $wpdb; - $and = wp_post_mime_type_where( $mime_type ); - $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); + $cache_key = sprintf( + 'attachments%s', + ! empty( $mime_type ) ? ':' . str_replace( '/', '_', implode( '-', (array) $mime_type ) ) : '' + ); - $counts = array(); - foreach ( (array) $count as $row ) { - $counts[ $row['post_mime_type'] ] = $row['num_posts']; + $counts = wp_cache_get( $cache_key, 'counts' ); + if ( false == $counts ) { + $and = wp_post_mime_type_where( $mime_type ); + $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); + + $counts = array(); + foreach ( (array) $count as $row ) { + $counts[ $row['post_mime_type'] ] = $row['num_posts']; + } + $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); + + wp_cache_set( $cache_key, (object) $counts, 'counts' ); } - $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); /** * Modifies returned attachment counts by mime type. diff --git a/wp-includes/version.php b/wp-includes/version.php index 50177d3957..cc39678976 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-54254'; +$wp_version = '6.1-alpha-54255'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.