wpseek.com
A WordPress-centric search engine for devs and theme authors
wp_count_attachments › WordPress Function
Since2.5.0
Deprecatedn/a
› wp_count_attachments ( $mime_type = '' )
| Parameters: |
|
| Returns: |
|
| Defined at: |
|
| Codex: |
Counts number of attachments for the mime type(s).
If you set the optional mime_type parameter, then an array will still be returned, but will only have the item you are looking for. It does not give you the number of attachments that are children of a post. You can get that by counting the number of children that post has.Related Functions: wp_count_comments, wp_insert_attachment, wp_attachment_is, wp_delete_attachment, wp_get_attachment_url
Source
function wp_count_attachments( $mime_type = '' ) {
global $wpdb;
/**
* Filters the attachment counts before the query is run.
*
* Returning a stdClass object short-circuits wp_count_attachments(),
* skipping both the object cache lookup and the database queries. This
* allows the counts to be served from another source, such as a value
* pre-computed in the background, when an exact real-time count is not
* required.
*
* If the filtered value does not contain a trash count, one is added to the
* result and set to zero. Missing MIME types are not added, as they depend on
* the attachments that exist. The filtered value should therefore contain a
* count for every MIME type matching `$mime_type` that has attachments,
* including types first uploaded after the value was computed, as callers
* may read the count for any MIME type that exists. It should also contain
* only MIME types matching `$mime_type`, with a trash count limited to
* attachments of those types, as the default query does.
*
* The returned value is not cached and passed through the
* {@see 'wp_count_attachments'} filter.
*
* @since 7.2.0
*
* @param stdClass|null $counts An object containing the attachment counts by
* mime type, or null to run the default query.
* Default null.
* @param string|string[] $mime_type Array or comma-separated list of MIME patterns.
*/
$counts = apply_filters( 'pre_wp_count_attachments', null, $mime_type );
if ( $counts instanceof stdClass ) {
$counts = clone $counts;
if ( ! isset( $counts->trash ) ) {
$counts->trash = 0;
}
/** This filter is documented in wp-includes/post.php */
return apply_filters( 'wp_count_attachments', $counts, $mime_type );
}
$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 );
$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' );
}
/**
* Filters the attachment counts by mime type.
*
* @since 3.7.0
*
* @param stdClass $counts An object containing the attachment counts by
* mime type.
* @param string|string[] $mime_type Array or comma-separated list of MIME patterns.
*/
return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type );
}