wppaste
WordPress

wp_count_attachments( string|string[] $mime_type = '' ): stdClass

Since
2.5.0
Source
wp-includes/post.php:3497
Counts number of attachments for the mime type(s).

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

Reaches the database via ->get_results().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
20–62

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 68.

Plugin surface
1 hook

Third-party callbacks on 'wp_count_attachments' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_get()called directly
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
$counts !== false20–25wp_cache_get(), apply_filters()
$counts === false56–62wp_cache_get(), wp_post_mime_type_where(), ->get_results(), ->get_var(), wp_cache_set(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev6820–624
8.56820–624
8.46820–62410 fewer instructions than PHP 8.3
8.37824–724
8.27824–724
8.17824–7241 fewer instruction than PHP 7.4
7.47924–734

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:

  1. apply_filters( wp_count_attachments )filterline 3529 (+32 into the body)

    Filters the attachment counts by mime type.

Uses · 4

Used by · 1

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.