Bear with me. I promise there’s a question when I’m done 🙂
In WordPress, you can upload images while editing a post and have the option to insert them into the content (or not).
Regardless, the image appears to be “attached” to the post when viewing the Media manager.
Does WordPress place any references into the database to differentiate an image that’s been attached and also inserted into the post content? Or conversely, an image that’s been attached to a post but has NOT been inserted into the post content?
Why do I want to know this?
I would like to allow the user to upload images that will be written above the post content (a row of images) by simply uploading them via the post attachment wizard.
But they may also upload some images that they only want to appear in the content, so they will click “insert into post” in that case. Unless I have some way of differentiating images that have actually been inserted vs images that have merely been attached, they will get duplicate images, one in the post and one at the top of the post.
Any thoughts on how to do this?
I suppose an alternate approach would be to add a checkbox to the attachments editor to flag an image for display as a post header attachment, then do a lookup for images attached to the post with the special flag.
I used nextgen gallery plugin to do something like this. Actually I’ve used only half of that: the end user uploads images through the plugin’s interface but the actual display inside the post is done with a custom shortcode.
I don’t think there is a way to tell if an attachment has been inserted in the post, short of examining the post content source.
**Updated snippet: had been using global $post ID and not the passed in ID, so it would take a couple updates before the meta tag was added to content images. Now it works.
I needed a similar solution and following on from Jan Fabry’s suggestions I came up with a function (admittedly inelegant, but it works):
So that gets pasted into (functions.php). Then I use the attachments meta value in a loop wherever I’m spitting out my images (per Jan Fabry):
This spits out all the attached images that are not inserted into the post content.
As Kemp said, there is no difference in the database between an image that has been attached, and one that is actually inserted into the content. I would scan the content (on post save, not every time you display it) and set an extra metadata attribute for attached-but-not-displayed images. Remember to check for galleries too: they are inserted with a shortcode, so it’s not always just a search for
<img>
tags.