Title: wp_count_attachments
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#changelog)

Counts number of attachments for the mime type(s).

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#parameters)󠁿

 `$mime_type`string|string[]optional

Array or comma-separated list of MIME patterns.

Default:`''`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#return)󠁿

 stdClass An object containing the attachment counts by mime type.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#source)󠁿

    ```php
    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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/post.php#L3559)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/post.php#L3559-L3592)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_count_attachments’, stdClass $counts, string|string[] $mime_type )](https://developer.wordpress.org/reference/hooks/wp_count_attachments/)

Filters the attachment counts by mime type.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_cache_set()](https://developer.wordpress.org/reference/functions/wp_cache_set/)`wp-includes/cache.php` |

Saves the data to the cache.

  | 
| [wp_post_mime_type_where()](https://developer.wordpress.org/reference/functions/wp_post_mime_type_where/)`wp-includes/post.php` |

Converts MIME types into SQL.

  | 
| [wp_cache_get()](https://developer.wordpress.org/reference/functions/wp_cache_get/)`wp-includes/cache.php` |

Retrieves the cache contents from the cache by key and group.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [wpdb::get_results()](https://developer.wordpress.org/reference/classes/wpdb/get_results/)`wp-includes/class-wpdb.php` |

Retrieves an entire SQL result set from the database (i.e., many rows).

  | 
| [wpdb::get_var()](https://developer.wordpress.org/reference/classes/wpdb/get_var/)`wp-includes/class-wpdb.php` |

Retrieves one value from the database.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#)

| Used by | Description | 
| [media_upload_library_form()](https://developer.wordpress.org/reference/functions/media_upload_library_form/)`wp-admin/includes/media.php` |

Outputs the legacy media upload form for the media library.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_count_attachments/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_count_attachments%2F)
before being able to contribute a note or feedback.