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
- How to query posts based on lat-lng coordinate as post meta?
- How to find attachment by it’s name?
- WP_Query for WooCommerce Products
- WP_Query with two post types, but requiring category on only one of those post types
- How do I delete all generated images from my server except those currently used in posts
- How to limit the posts
- How to order posts in wp_query by a meta_value of the corresponding author
- Get the post thumbnail with wp_query
- Multiple instances of Featured Image Query
- WPDB query – decrypting DB data
- wp_query ignoring custom post_status
- Query posts(CPT, pages , hierarchical) by Ancestor ID
- Input check to custom post type query
- WP_Query orderby
- How to check if logged in user have pending custom post?
- How can I re-query post_type and rewrite the url?
- Delete attached images from posts
- How can I show only last year posts in wordpress archive?
- I can’t get post based on its postmeta value and key
- Combine Tax Archive and Meta_Query in WP_Query
- Get posts by birthday
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- Pagination not working with WP_QUERY
- Extract video content from post to display on front page
- Carousel slider with WP_Query to show 3 posts on each slide
- how to use transient method?
- How to replecate the _fields parameter in custom REST api endpoint
- taxonomy-{term}.php terms pagination returning 404 after a certain page
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2
- Prevent URL Parameter Affecting other WP Query