Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Check whether attachment ID is associated with more than one post?

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:

  1. Getting attachments by meta value
  2. How to set author for post AND post attachments
  3. Multi-line captions on attachments
  4. Get attachment by meta_key value
  5. How to update the title of audio file through its attachment metadata
  6. WordPress Attachments vs Post Meta
  7. How To Retrieve An Image Attachment’s Alt Text?
  8. How to manage attachment relationships for specific posts in WP 3.5+
  9. Can I add a Category Metabox to attachment?
  10. Change attachment filename
  11. How to get attachment file name not attachment URL
  12. How to trigger a refresh in the media modal
  13. Use REGEXP in WP_Query meta_query key
  14. Correctly delete posts with meta and attachments [duplicate]
  15. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  16. Get all post attachments except featured image
  17. Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails
  18. getting attachement images src and add classes
  19. How can I bulk delete media and attachments using WP-CLI?
  20. Getting a taxonomy’s thumbnail url
  21. Check if post has attachments (not image)
  22. How to cleanly delete attachment but retain original file?
  23. Changing attachment urls?
  24. Add custom class to attachment in media library grid mode
  25. Are captions stored anywhere?
  26. Trying to hide buttons from Attachment window
  27. get_the_post_thumbnail() doesn’t taking style attribute
  28. Expanding new Media Uploader in WordPress 3.5
  29. How can I UN-attach media from a post?
  30. Uploading Multiple Attachments From Front-End With A Description
  31. Validate alt text for attachments?
  32. complex restriction of items in media library
  33. Remove duplicate attachments
  34. Edit image / Delete image link
  35. How could I add button next to “edit image” button in “Attachment Details”
  36. Applying automatic link class to images embedded to posts
  37. Meta compare with date (stored as string) not working
  38. How to get attachment caption (get_the_excerpt gives parent post excerpt) ?
  39. Is it possible to query specific WordPress Attachment files (.ppt & .pdf) and output them in a list?
  40. media_sideload_image file name?
  41. Get all posts (of any post type) an attachment is used in
  42. Ordering posts having multiple post-meta date fields
  43. Detect if is image unattached
  44. How to single click to download image in single post
  45. delete attachment from front end
  46. Custom Queries: Joining On Meta Values From Two Custom Post Types
  47. Unattaching unlinked images
  48. How to get attachments for a specific post type?
  49. Give attachments an archive page
  50. custom fields for attachments?
  51. Insert Media – Attachment – Link to : Remove the “attachment page” option
  52. Show Post ID in “Find Posts or Pages” box in Media Library?
  53. URL rewrite with add_rewrite_rule and attachment_id
  54. How Can I save multiple records in same meta key?
  55. How to add media from front-end to an existing post?
  56. Can I attach a document (eg: PDF) but have a JPEG as a thumbnail?
  57. Retrieving a custom link on an attachment
  58. Meta query interfering with orderby relevance
  59. Get attachment from post
  60. WooCommerce conditional meta query
  61. How can I display image metadata?
  62. How can I display custom fields value from attachment media?
  63. Delete attachments from Front end
  64. Add column for attachment file size
  65. Creating a multi-file upload form on the front end for attachments
  66. how to test for attached image
  67. How to update single value in multi dimensional Post Meta?
  68. Unable to upload specific .zip file type via frontend in WordPress 4.0
  69. Get attachments (get_posts) and WP 3.5 new uploader
  70. get post attachments of a particular size
  71. What is an efficient way to query based on post_meta?
  72. Change image link: wp_get_attachment_link
  73. Make individual attachments private?
  74. exposing attachment uploading to the front end — delicate
  75. Gel all image from certain post type
  76. Making images from single.php pointing to an attachment .php template
  77. Add_Filter example for wp_get_attachment_link
  78. WP_Query not working as expected for attachments and custom meta_query
  79. I can’t set meta_key in my custom post type query
  80. How to get meta value in wp_attachment_metadata
  81. Documentation for post meta
  82. Change the Permalink for wordpress attachment
  83. How to upload image from front end and save in media library?
  84. Can i hide the attachments from media which I uploaded from front end?
  85. ID of images from gallery
  86. Get the attachment URL on single.php
  87. How to get list of paths (not urls) for an image
  88. Display info from custom fields in all images’ HTML
  89. How can I create a WP_Query that returns posts where one meta_value
  90. Comparisson between date() and a date from wp_postmeta
  91. wp_get_attachment_image_src always returns false
  92. Exact image sizes
  93. WordPress altering my custom query, How to fix it?
  94. Ordering posts by anniversary using only day and month
  95. WP Meta Query for some meta (array) values
  96. Can’t sort order of wp_query with 2 meta keys
  97. attachment url rewrite
  98. Get the size (size in Kb or MB) of an featured image?
  99. get_post_meta causes database queries
  100. copy attachments to another post type and change attachment url
Categories attachments Tags attachments, meta-query, post-meta
store an array of all the terms existing
Php conditional help needed

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress