Support » Plugins » Hacks » Remove image if identical with featured image

  • In many of my early blog posts there is an image embedded which is also set as the featured image. If this is the case, the visitors see the image twice now.

    So I want to remove all images from posts if they are identical with the respective featured image. Is there any way to do this?


Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    There is, but it can get complicated when images are inserted in a post from the media library because the image’s attachment record is unrelated to the post in question. Another complication is when images used are different sizes, thus the filenames do not match though they are the same imagery. Removing img tags from content is further complicated when captions are used. You may be just removing redundant image references, but removing captions is actually destroying data.

    The basic approach is to query for all posts with a featured image, checking for redundant use either from attachments or within content, and when found remove the reference. Exactly how would depend on which complications need to be addressed.

    Hi bcworkz,

    thanks for your reply!

    I don’t understand the first complication. Images are always inserted from the media library, aren’t they?

    The sizes are the same, so the file names always match. Captions are not used.

    I’d really appreciate your further help.


    Moderator bcworkz


    After re-reading my post I see I was not very clear about the first complication. The complication is from when an image that was initially uploaded for a different post gets reused for a new post by picking it from the image library, as opposed to uploading a new image from offsite with the upload files tab. The new post would then have no attachment representing that image, preventing a useful type of query. In addition, it’s possible for images to be added, usually by some special plugin, bypassing the media manager entirely.

    Another complication that occurred to me is if the image in content is linked to a full size image or otherwise linked such that the link needs to be removed along with the image.

    Assuming the image was added anew for the post in question, typically the most common scenario, the process would work like this:
    1. Query for all posts with postmeta key ‘_thumbnail_id’
    2. For each post found, get the image ID of the featured image.
    3. Find the related image reference in post content and remove it.
    4. Update the modified post.

    Test carefully before turning this script loose on your entire DB!

    The real trick here is accurately identifying the image reference and removing it without accidentally deleting other data for all the various contexts that it may be found in, or not fully removing related content, resulting in invalid HTML. You might try to find an img tag that has the class “wp_image_{$image_id}”. Another approach would be to determine the base filename and search the content for occurrences of that string, removing the related tags when found. Exactly which and how would depend on what context variations you expect to encounter.

    Unfortunately, I have no SQL skills. I also couldn’t find any websites on which the process is described for laymen.

    So I’d really appreciate it if you (or somebody else) could give me the code for this :))

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Remove image if identical with featured image’ is closed to new replies.