featured-image-wordpress

How to set post first image as featured image automatically

From wordpress 3.0 version wordpress launched the feature called featured image. Many new wordpress themes are compatible with new wordpress version. So using or choosing the image as featured image for post is another manual work we need to do.

Many old wordpress website holder or blogger is having issue with this functionality. What they want is when they create the post they haven’t set the featured image, but they would like the featured image to default to the image that has been included in the post.

Some wordpress always use the first image as featured image but old post was not updated with featured image. They need to do manual work to set the featured image. I also faced same issue.

After doing some R&D I found the solution. If you want to set the your posts first image as featured image then use my following code.

For using the code you need to open your mysql database command prompt or phpmysqladmin program and select your wordpress database and execute the following query in that.

insert into wp_postmeta (meta_value, meta_key, post_id) select DISTINCT(ID), post_type , post_parent from wp_psts where post_type= 'attachment' and post_parent !=0 and post_status='inherit';<br /><br />update wp_postmeta set meta_key = '_thumbnail_id' where meta_key='attachment'

Using above sql query you will be able to set the featured image to your old and new post from post content. If you are having any issues with using this sql then write to me.

How to set post first image as featured image automatically

How to set post first image as featured image automatically

  • http://www.RegScheepers.com Reg Scheepers

    Thanks brother. You’ve saved me some time here.

  • Chris Slee

    Couldn’t this be a one liner?

    insert into wp_postmeta (meta_value,meta_key,post_id)
    select DISTINCT(ID),’_thumbnail_id’,post_parent
    from wp_posts
    where post_type= ‘attachment’
    and post_parent !=0
    and post_status=’inherit';

  • http://sleech.myopenid.com/ Chris Slee

    Couldn’t this be a one-liner?

    insert into wp_postmeta (meta_value,meta_key,post_id)
    select DISTINCT(ID),’_thumbnail_id’,post_parent
    from wp_posts
    where post_type= ‘attachment’
    and post_parent !=0
    and post_status=’inherit';

  • http://www.bigdigi.net Aaron Page

    For those running WordPress in Windows environment….

    I used the MySQL workbench and added:

    USE ‘[your DB name here]‘;

    before the code above. I hope this helps someone out.

  • a1234my

    nice work, but this dosnt get the first image. just the attachment on the post

  • http://www.seosamui.com John Peden

    Very clever but as my friend said it just gets the post attachment.

    Is there a way of identifying the first img src value and assigning that to be the featured image? Obviously that makes it a bit trickier as the image needs to be downloaded from a remote site and uploaded as a featured image…

    Would be helpful for folks like me who need to assign featured images to nearly 600 imported blogger posts!

    • Mustafa Hanif

      Same problem with me, see if you can find a solution

  • http://businesscardorder.info Florinda Wolfson

    Awesome post. I so good to see someone taking the time to share this information

  • http://affiliatetown.info Dave Bertagna

    Great read. Thanks for the info!

  • http://www.bestappliancesreview.com Jody

    Been trying to find the solution for this like ages, especially after wordpress popular posts plugin decided to use the featured image thumbnail with its newest version. Thus, I will need to generate the featured image for all my old posts.

    Considering I have to update my database, I’m a bit reluctant to do it. Do you have any suggestion of wordpress plugin that can handle this automatically?

  • http://travel4leisure.info Audria Wamble

    I REALLY liked your post and blog! It took me a minute bit to find your site…but I bookmarked it. Would you mind if I posted a link back to your post?

  • http://www.thesinglegirlsguidetomen.com nikbanks

    where in your template are you supposed to place this code and this doesn’t define the database that you’re using so how does it know. thank you.

    • http://wordpressapi.com Wordpress API

      this is not a code which need to place in template.. you need to execute sql quries in database.

  • http://www.guatewireless.org fher98

    Mmmm…. i was looking for a plugin but ok gonna see if it works….

  • http://design.markcbain.com Mark

    I think you just saved my life…

  • SC123

    This code worked perfectly and saved me so much pain. Thanks!

  • http://www.zeroex.com/ ZeroEx

    Thanks,man this is very usefull and can save you a lot of time.I had a problem with the featured image because i can’t save it and this trick saved me! Thanks again!

  • http://howcan-i-getmoney.com Rahul

    Thanks friend, you really saved lots of hours for me, i m very grateful to you

  • http://www.tomeboshevski.com Tome

    Thank you, this has been very helpful to me, but I think you need to update the code so it can work with copy/paste.

    You need to insert a semi-colon (;) at the end of the second command so can be executed properly.

    update wp_postmeta set meta_key = ‘_thumbnail_id’ where meta_key=’attachment';

  • http://mattlebrun.com Matt

    How do you update this to work with a different image? Say your post doesn’t have any images on it but you wanted a featured image set to it anyway.