Count the number of images uploded on the website

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 Comment