How to Count the Total Number of JPG, GIF, and PNG Images in the WordPress Media Library

WPTurbo » Snippets » How to Count the Total Number of JPG, GIF, and PNG Images in the WordPress Media Library
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever wondered how many JPG, GIF, and PNG images you have in your WordPress media library? Knowing the total number of these image file types can be helpful for various reasons, such as optimizing your website’s performance or categorizing your media content. In this article, we will guide you through the process of counting the total number of JPG, GIF, and PNG images in your WordPress media library using a simple function. With this knowledge, you can better manage and utilize your media library to enhance your website’s overall user experience.

					function wpturbo_count_image_types() {
    $image_types = array('jpg', 'jpeg', 'gif', 'png');
    $count = 0;
    $media_query = new WP_Query(array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'posts_per_page' => -1,
    ));

    if ($media_query->have_posts()) {
        while ($media_query->have_posts()) {
            $media_query->the_post();
            $file_type = wp_check_filetype(get_the_guid());

            if (in_array($file_type['ext'], $image_types)) {
                $count++;
            }
        }
    }

    wp_reset_postdata();

    return $count;
}
$total_images = wpturbo_count_image_types();
echo 'Total number of JPG, GIF, PNG images in the media library: ' . $total_images;
				

In this section, we will explain how the code snippet works to count the total number of JPG, GIF, and PNG images in the media library.

The first step is to define a new function called wpturbo_count_image_types(). This function will be responsible for retrieving the image attachments from the WordPress database and calculating the count of JPG, GIF, and PNG images.

Inside the function, we define an array called $image_types that contains the file extensions we want to count. In this case, it includes ‘jpg’, ‘jpeg’, ‘gif’, and ‘png’.

Next, we initialize a variable called $count to keep track of the total count of images. This variable will be incremented whenever an attachment matches one of the specified image types.

We create a new WP_Query instance called $media_query to retrieve all attachments in the media library that have a post_mime_type of ‘image’. We set the posts_per_page argument to -1 so that all attachments are returned.

We then check if the $media_query has any posts using the have_posts() method. If it does, we enter a while loop that iterates through each attachment.

In each iteration, we use the the_post() method to set up the current attachment’s data. We then retrieve the file type of the attachment using the wp_check_filetype() function on the attachment’s GUID.

We use the in_array() function to check if the file extension of the attachment matches one of the image types specified in the $image_types array. If it does, we increment the $count variable by one.

After looping through all the attachments, we use the wp_reset_postdata() function to reset the $media_query object and restore the global post data to its original state.

Finally, we return the value of the $count variable, which represents the total number of JPG, GIF, and PNG images in the media library.

To display the total count, we call the wpturbo_count_image_types() function and assign its return value to the $total_images variable. Then, we use the echo statement to output the total count with a descriptive message.

That’s it! This code snippet effectively counts the total number of JPG, GIF, and PNG images in the media library and displays it on the page.

Register an account to save your snippets or go Pro to get more features.