Exclude images from “inserted into post” when trying to get attachments

I had a similar problem a while ago and helped answer it here. I didn’t have an account at the time though and can’t find the original post otherwise I’d link it. If I recall, Jan Fabry pretty much gave me marching orders and I hacked something out. Mind, I don’t know the exact nature of how you’re feeding images to the coin slider. I imagine you’re looping through your get_posts results and spitting out images one at a time. Anyway, here’s some code. Pretty ugly, but it works.

So first you use the ‘wp_insert_post’ hook to add a unique meta value to any image that’s inserted into the post content.

add_action('wp_insert_post', 'insertedImage_save_meta');

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

    $rawPostDoc = new DOMDocument();
    @$rawPostDoc->loadHTML($upPost->post_content);                     
    $imgs = $rawPostDoc->getElementsByTagName('img');

    foreach($imgs as $img){
        $imgIDStr = substr($img->getAttribute('class'), (stripos($img->getAttribute('class'),'wp-image-')+9), 8);
        $imgID = preg_replace("[^0-9]", '', $imgIDStr);

        if($imgID !== false && $imgID !== '') { // double falsy check because of specific nature of stripos() returns coupled with the preg_replace return. Not sure if this is necessary.

            if(get_post_meta($imgID, '_inserted-image', true) === '')
                update_post_meta($imgID, '_inserted-image', 'yes');

        }

    }

}

Then for your display, when looping through your image objects you’d check for that unique meta with a get_post_meta() call and neglect to spit out any html if you find it.

So if I’m looping through my get_posts results with foreach( $images as $image ), I’d do this:

if ( get_post_meta( $image->ID, '_inserted-image', true ) === 'yes' )
    continue;

LIMITATIONS: This will not remove the assigned meta value from images if the inserted image is later removed from the post content but stays in the post’s gallery. But the function could pretty easily be expanded to check against all the post’s attached images and remove the meta tag if an image is not found in the body content.