Media: Add caching to `wp_count_attachments()`.
This changeset adds caching to `wp_count_attachments()`, for better consistency with `wp_count_posts()`. Props jeherve, antpb, mukesh27, robinwpdeveloper, costdev. Fixes #55227. Built from https://develop.svn.wordpress.org/trunk@54255 git-svn-id: http://core.svn.wordpress.org/trunk@53814 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
c58963b07f
commit
5dcad0a618
|
@ -3090,6 +3090,13 @@ function wp_count_posts( $type = 'post', $perm = '' ) {
|
|||
function wp_count_attachments( $mime_type = '' ) {
|
||||
global $wpdb;
|
||||
|
||||
$cache_key = sprintf(
|
||||
'attachments%s',
|
||||
! empty( $mime_type ) ? ':' . str_replace( '/', '_', implode( '-', (array) $mime_type ) ) : ''
|
||||
);
|
||||
|
||||
$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 );
|
||||
|
||||
|
@ -3099,6 +3106,9 @@ function wp_count_attachments( $mime_type = '' ) {
|
|||
}
|
||||
$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' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies returned attachment counts by mime type.
|
||||
*
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue