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.
Related Posts:
- How to paginate attachments in a secondary query as gallery?
- How do I exclude all images from a wp_query?
- get images attached to post
- query attachments of parent page if attachments of current page are smaller than …
- wp_query for displaying attachments with a tag
- WP_Query: attachment image in “full” size?
- wp_get_attachment_image not to get the post_thumbnaill
- How To Query All Attachment Images Found In Post Galleries
- Some images not being returned with wp_get_attachment_image
- Broken? WP_Query and “attachment” as a post type
- Return only Count from a wp_query request?
- Is it possible to query all posts that don’t have an attachment?
- How to paginate wordpress [gallery] shortcode?
- How to get Page/Post Gallery attachment images in order they are set in backend using WP_Query()?
- Getting attachments by meta value
- count posts from custom taxonomy terms by year
- Get attachment by slug
- Get attached media only
- How to use global post counter in the loop?
- How to display images in related posts?
- post thumb nail
- Count total number of post in foreach loop
- WP Query search for attachments and their exact title
- Show selected images on top in Media Manager
- How can I query posts with newly uploaded images?
- Sorting By Custom Posts With Attachments
- Attachment changing page’s permalink – $post not resetting?
- How to fetch only media that was already attached to a post/page?
- How to do meta_query for attachments?
- Get attachment by meta_key value
- Getting the current post number / make posts increment
- Fastest way of counting posts of a custom post type in a specific taxonomy term?
- How to use wp_query to post count by search term > group post count by date > convert to json. (ex. result 12-21-2020 – 343)
- How to show optimized list of posts with all their attachment images
- Where to put meta Keys
- WP_Query Attachment adds additional attachment count and need to link attachment to post url
- How to count posts with specific arguments
- Better wordpress attachment query than this
- Why having more than 10 clauses in WP_Query results in some outputs being dropped?
- wp_query get attachments in larger size
- Creating attachments archive in tags and categories
- How to add WordPress featured image via SQL
- Display all attached image of every post of custom post type and link to original post
- 2 queries with counters
- Ignore image urls in wp_query search
- Insert images into wordpress post with a query
- Find only those galleries with images
- Having a variable based on $post_count increase when i move to the next page of results
- How can I build a query that returns all attachments of a page and it’s children pages?
- get all images from the wordpress media library with link to the post they are associated with
- ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)
- Counting instances of words in the results of a post query
- Query posts by post type, author and post meta
- order posts by a secondary query that counts items
- How to restrict add media library only to images of the same post family?
- how to get custom attachment url?
- Change database image location for transportability
- WP query retrieve the src of attached image
- Pull posts from all categories if quantity is not met?
- counting post from a wp-query using sticky_post
- Trying to use “Medium” featured image for custom post type on home page
- Query All Attachments and Order by Parent Publish Date
- Display Count of posts
- Display images with same specific value?
- WP_Query for attachments without duplicating post_parent and displaying tagged image
- Using WP_Query to get attachment returns empty set
- Pull Instagram images into an existing loop?
- Loop issues when creating custom query for media uploader
- Give attachments an archive page, and exclude unattached ones
- on attachment.php, how to display previous and next attachment links that follow the same order as a custom WP Query
- WP_Query based on URL parameters
- Audio or playlist shortcode condition according to the amount of files on attachment page
- Order by meta_key in custom post type doesn’t affect the query
- Is there a PHP function that can match anything after the given URL, so my IF statement will work in each instance?
- WP query exclude post within the last month / only show over 1 month old
- WordPress query for most recent posts from multiple categories
- Pass array of taxonomy terms to wp_query
- Editing the default wordpress search
- How can I get the number of custom post type posts that have a specific attachment image set?
- Slow page loads due to WordPress Core Query
- Adding column to wp_posts table or join wp_posts table with another table
- How to create resized versions of images added with wp_insert_attachment
- WP_Query for a taxonomy with different taxonomy types
- Add category to my get_post query
- How to change an image source when rendering on frontend?
- How to use custom page for all posts with custom url, call another directory?
- cron job for custom post type not working since WordPress 5
- WP_Query – multiple orderby with a rand field, doesn’t work
- What does $temp do?
- Insert wp_query after the_content with plugin (filter the_content won’t work)
- Is instantiating WP_Query not possible within an admin Ajax call?
- Calling body_class and wp_nav_menu for a different post
- Category Archive not working for pages
- Custom query on custom table fails
- Get closest event where meta_key field is an array
- Can’t get the_content to show
- Meta_query weird behaviour
- Modify query after meta value
- Sort wp_query of two post types using meta datefield for one and date for the other – possible?
- Get field added via attachment_fields_to_edit filter in Gutenberg