I have here the coin-slider which takes all pictures from the gallery. I get the attachments through the following code:

$attachments = get_posts(array('post_type' => 'attachment', 'post_parent' => $post->ID));

But this function also gives me pictures, which were posted inline on a page. If I look to article image I can see the picture in the tab gallery. The picture is also shown in the editor.

How can I exclude pictures which where inserted into the editor?

I've seen that there is an exclude option for get_posts, but neither get_the_post_thumbnail(); nor get_post_thumbnail_id(); does work for me. The image is still included in the slider and the post.


It seems that it impossible to place a picture in a post without putting it in the gallery. If it is in the gallery it is also in the result of get_posts. Now I have uploaded a file per FTP on the webserver and placed the image by using an URL. Now it seems to work, but it isn't an acceptable solution ...

1 Answer 1


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();
    $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' )

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.

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.