you can use following way
$attachment_id = 5; //put here you attachment id
$sql = "SELECT count(DISTINCT pm.post_id) FROM $wpdb->postmeta pm JOIN $wpdb->posts p ON (p.ID = pm.post_id) WHERE pm.meta_key = '_thumbnail_id' AND pm.meta_value="$attachment_id" AND p.post_type="post" AND p.post_status="publish"";
$count = $wpdb->get_var($sql);
Related Posts:
- Getting attachments by meta value
- How to set author for post AND post attachments
- Multi-line captions on attachments
- Get attachment by meta_key value
- How to update the title of audio file through its attachment metadata
- WordPress Attachments vs Post Meta
- How To Retrieve An Image Attachment’s Alt Text?
- How to manage attachment relationships for specific posts in WP 3.5+
- Can I add a Category Metabox to attachment?
- Change attachment filename
- How to get attachment file name not attachment URL
- How to trigger a refresh in the media modal
- Use REGEXP in WP_Query meta_query key
- Correctly delete posts with meta and attachments [duplicate]
- What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
- Get all post attachments except featured image
- Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails
- getting attachement images src and add classes
- How can I bulk delete media and attachments using WP-CLI?
- Getting a taxonomy’s thumbnail url
- Check if post has attachments (not image)
- How to cleanly delete attachment but retain original file?
- Changing attachment urls?
- Add custom class to attachment in media library grid mode
- Are captions stored anywhere?
- Trying to hide buttons from Attachment window
- get_the_post_thumbnail() doesn’t taking style attribute
- Expanding new Media Uploader in WordPress 3.5
- How can I UN-attach media from a post?
- Uploading Multiple Attachments From Front-End With A Description
- Validate alt text for attachments?
- complex restriction of items in media library
- Remove duplicate attachments
- Edit image / Delete image link
- How could I add button next to “edit image” button in “Attachment Details”
- Applying automatic link class to images embedded to posts
- Meta compare with date (stored as string) not working
- How to get attachment caption (get_the_excerpt gives parent post excerpt) ?
- Is it possible to query specific WordPress Attachment files (.ppt & .pdf) and output them in a list?
- media_sideload_image file name?
- Get all posts (of any post type) an attachment is used in
- Ordering posts having multiple post-meta date fields
- Detect if is image unattached
- How to single click to download image in single post
- delete attachment from front end
- Custom Queries: Joining On Meta Values From Two Custom Post Types
- Unattaching unlinked images
- How to get attachments for a specific post type?
- Give attachments an archive page
- custom fields for attachments?
- Insert Media – Attachment – Link to : Remove the “attachment page” option
- Show Post ID in “Find Posts or Pages” box in Media Library?
- URL rewrite with add_rewrite_rule and attachment_id
- How Can I save multiple records in same meta key?
- How to add media from front-end to an existing post?
- Can I attach a document (eg: PDF) but have a JPEG as a thumbnail?
- Retrieving a custom link on an attachment
- Meta query interfering with orderby relevance
- Get attachment from post
- WooCommerce conditional meta query
- How can I display image metadata?
- How can I display custom fields value from attachment media?
- Delete attachments from Front end
- Add column for attachment file size
- Creating a multi-file upload form on the front end for attachments
- how to test for attached image
- How to update single value in multi dimensional Post Meta?
- Unable to upload specific .zip file type via frontend in WordPress 4.0
- Get attachments (get_posts) and WP 3.5 new uploader
- get post attachments of a particular size
- What is an efficient way to query based on post_meta?
- Change image link: wp_get_attachment_link
- Make individual attachments private?
- exposing attachment uploading to the front end — delicate
- Gel all image from certain post type
- Making images from single.php pointing to an attachment .php template
- Add_Filter example for wp_get_attachment_link
- WP_Query not working as expected for attachments and custom meta_query
- I can’t set meta_key in my custom post type query
- How to get meta value in wp_attachment_metadata
- Documentation for post meta
- Change the Permalink for wordpress attachment
- How to upload image from front end and save in media library?
- Can i hide the attachments from media which I uploaded from front end?
- ID of images from gallery
- Get the attachment URL on single.php
- How to get list of paths (not urls) for an image
- Display info from custom fields in all images’ HTML
- How can I create a WP_Query that returns posts where one meta_value
- Comparisson between date() and a date from wp_postmeta
- wp_get_attachment_image_src always returns false
- Exact image sizes
- WordPress altering my custom query, How to fix it?
- Ordering posts by anniversary using only day and month
- WP Meta Query for some meta (array) values
- Can’t sort order of wp_query with 2 meta keys
- attachment url rewrite
- Get the size (size in Kb or MB) of an featured image?
- get_post_meta causes database queries
- copy attachments to another post type and change attachment url