I mention “full website” because I don’t need the number of attachments on one post (as answered in many questions). I basically need a function that returns the number of uploaded images (attached and unattached) on the website, excluding non-image files.

I’ve done some research, but there isn’t any direct function that counts only the images. What is the most simple way to do this?

2 Answers
2

There’s a handy built-in function, namely wp_count_attachments().

We can filter out images with wp_count_attachments( $mime_type="image" ) that returns an object like:

stdClass Object
(
    [image/gif] => 9
    [image/jpeg] => 121
    [image/png] => 20
    [image/x-icon] => 6
    [trash] => 0
)

So we can use the one-liner:

$count = array_sum( (array) wp_count_attachments( $mime_type="image" ) );

for the total number of images.

Leave a Reply

Your email address will not be published. Required fields are marked *