wp_count_attachments( string|string[] $mime_type = '' ): stdClass
- Since
- 2.5.0
- Source
wp-includes/post.php:3431
Description
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.
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$mime_typestring|string[]optional- Array or comma-separated list of MIME patterns. Default empty.Default:
''
Return value
stdClass- An object containing the attachment counts by mime type.
Performance profile
How much work a call to wp_count_attachments() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 20–62
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via ->get_results().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 68.
Third-party callbacks on 'wp_count_attachments' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get()called directly - hookthird-party callbacks
apply_filters()called directly - sqldatabase query
->get_results()called directly
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_count_attachments() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$counts !== false | 20–25 | wp_cache_get(), apply_filters() |
$counts === false | 56–62 | wp_cache_get(), wp_post_mime_type_where(), ->get_results(), ->get_var(), wp_cache_set(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 68 | 20–62 | 4 | |
| 8.5 | 68 | 20–62 | 4 | |
| 8.4 | 68 | 20–62 | 4 | 10 fewer instructions than PHP 8.3 |
| 8.3 | 78 | 24–72 | 4 | |
| 8.2 | 78 | 24–72 | 4 | |
| 8.1 | 78 | 24–72 | 4 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 79 | 24–73 | 4 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wp_count_attachments() runs, in this order:
- apply_filters( wp_count_attachments )filterline 3463 (+32 into the body)
Filters the attachment counts by mime type.
Uses · 4
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- wp_post_mime_type_where()Converts MIME types into SQL.
- wp_cache_set()Saves the data to the cache.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 1
- media_upload_library_form()Outputs the legacy media upload form for the media library.
Source code
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 ); $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 );}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.