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.

3 Answers 3


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):

    /* Add meta value to distinguish inserted images from other attached images - lionassociates.com */

    function insertedImage_save_meta($post_id) {
        $upPost = get_post($post_id);

        $rawPostDoc = new DOMDocument();
        $imgs = $rawPostDoc->getElementsByTagName('img');
        $i = 0;
        foreach($imgs as $img){
            $imgIDStr = substr($img->getAttribute('class'), (stripos($img->getAttribute('class'),'wp-image-')+9), 6);
            $imgID = preg_replace("[^0-9]", '', $imgIDStr);
            if($imgID != '') {
                if(get_post_meta($imgID, '_inserted-image', false))
                    update_post_meta($imgID, '_inserted-image', 'yes');
                    add_post_meta($imgID, '_inserted-image', 'yes');
    /* Do something with the data entered */
    add_action('save_post', 'insertedImage_save_meta');

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):

    $imgs = get_posts(array('post_parent'=>get_the_id(), 'numberposts'=>-1, 'post_type'=>'attachment', 'post_mime_type'=>'image'));
    if($imgs[0]) {
        foreach($imgs as $img) {
            if(!get_post_meta($img->ID, '_inserted-image', true))
            echo wp_get_attachment_image($img->ID, 'sec-slide'); //'sec-slide' is just the name of a custom image size

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.