WordPress Shortcode to Get Total Number of Images

Srivishnu Ramakrishnan
Srivishnu Ramakrishnan
Engineer & Web Creator
LinkedInTwitter

Here's a shortcode to get the total number of images in your WordPress site:

Shortcode:[total_images]

Code for the shortcode

php
//Total Images in Site
function total_images_count(){
    $args = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
    );
    $query_images = new WP_Query($args);
    return $query_images->post_count;
}
add_shortcode('total_images', 'total_images_count');

Wherever you want to display the total number of images in your site, you can simply use the [total_images] shortcode.

How to add this code to your WordPress site

How to Add PHP Code to WordPress