5 Responses

  1. Daniel Dvorkin
    Daniel Dvorkin January 25, 2013 at 12:57 pm Reply

    Oh, I had a very similar issue a few months ago. This would do the same trick, very quick:

    DELETE FROM wp_postmeta
    WHERE meta_key = '_thumbnail_id';

    INSERT INTO wp_postmeta
    (post_id,
    meta_key,
    meta_value)
    SELECT post_parent,
    '_thumbnail_id',
    Min(ID)
    FROM wp_posts
    WHERE post_type = 'attachment'
    AND post_mime_type LIKE '%image%'
    AND post_parent > 0
    AND post_parent NOT IN (SELECT post_id
    FROM wp_postmeta
    WHERE meta_key = '_thumbnail_id')
    GROUP BY post_parent;

  2. Daniel Dvorkin
    Daniel Dvorkin January 25, 2013 at 1:01 pm Reply

    Oh, I had a very similar issue a few months ago. This would do the same trick, very quick:

    DELETE FROM wp_postmeta
    WHERE meta_key = '_thumbnail_id';

    INSERT INTO wp_postmeta
    (post_id,
    meta_key,
    meta_value)
    SELECT post_parent,
    '_thumbnail_id',
    Min(ID)
    FROM wp_posts
    WHERE post_type = 'attachment'
    AND post_mime_type LIKE '%image%'
    AND post_parent > 0
    GROUP BY post_parent;

    and adding:
    AND post_parent NOT IN (SELECT post_id
    FROM wp_postmeta
    WHERE meta_key = '_thumbnail_id')

    to the last SELECT will do this only if the post doesn’t have a featured image already.

  3. skaapoom
    skaapoom February 14, 2013 at 11:04 am Reply

    Thanks, this saved my life!

  4. VIVEK KUMAR
    VIVEK KUMAR March 3, 2013 at 10:44 am Reply

    Thanks,
    I’m facing the same problem.
    i need this code more than anything…

  5. justanotherblogger
    justanotherblogger March 18, 2013 at 10:38 am Reply

    I just transferred my blog and have an problem. My images are not showing up in the posts and my featured image is not showing up. I have tried fixes,plugins is there a code I can use or a plugin that will fix the problem. Thanks

Leave a Reply

Comment Policy

By submitting a comment here, you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you use your site name, company name, or something completely random, we'll likely change it to whatever we want.

%d bloggers like this: