How to Display the Total Number of PDF Files in Your WordPress Media Library

Home » Snippets » How to Display the Total Number of PDF Files in Your 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 found yourself searching through your WordPress media library to count the number of PDF files you have? It can be a tedious task, especially if you have a large collection of files. But fear not, because in this article, we’ll show you how to easily display the total number of PDF files in your media library. With just a few simple steps, you’ll be able to quickly and accurately determine the number of PDF files in your library, saving you time and effort in the process. So let’s get started and make managing your media library a breeze!

					function wpturbo_display_total_pdf_count() {
    $args = array(
        'post_type'      => 'attachment',
        'post_mime_type' => 'application/pdf',
        'post_status'    => 'inherit',
        'posts_per_page' => -1,
    );

    $query = new WP_Query( $args );

    $count = $query->found_posts;

    echo "<p>Total PDF files in your media library: " . $count . "</p>";
}
add_action( 'admin_notices', 'wpturbo_display_total_pdf_count' );
				

The code snippet provided in this article allows you to display the total number of PDF files in your WordPress media library. This can be useful if you want to keep track of how many PDF files you have uploaded to your website.

Let’s break down the code step by step to understand how it works.

The first part of the code defines a function called wpturbo_display_total_pdf_count(). This function will be responsible for calculating and displaying the total number of PDF files in the media library.

Inside the function, we define an array called $args that will be used in the WP_Query to retrieve the PDF files from the media library. The array specifies the parameters for the query, including the post type (attachment), post MIME type (application/pdf), post status (inherit), and the number of posts to retrieve (-1 to retrieve all posts).

Next, we create a new instance of the WP_Query class using the $args array. This performs the query to retrieve the PDF files from the media library based on the specified parameters.

We then retrieve the total number of PDF files found by accessing the found_posts property of the WP_Query object and assigning it to the variable $count.

Finally, we output the total number of PDF files in the media library by echoing an HTML paragraph element (<p>) with the string "Total PDF files in your media library: " concatenated with the value of $count.

To display this information on the WordPress admin dashboard, we use the add_action() function to hook the wpturbo_display_total_pdf_count() function into the admin_notices action. This ensures that the total PDF count is displayed on the admin dashboard whenever WordPress renders its admin notices.

By adding this code snippet to your WordPress theme’s functions.php file or in a custom plugin, you will be able to easily display the total number of PDF files in your media library. This can be a helpful tool for managing and organizing your files.

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