Set First Image as Featured Image for all wordpress posts

S

Featured ImageWordPress recently from its version 3.0 have started a new feature called Featured Image where we need to set Featured Image for every individual post manually before we are going to publish the post. Although it is a great feature many of our previous posts doesn’t have featured image and day by day many new themes have started using this feature because of which we may miss some design aspects of the new theme because of not having a featured image.

To work around this problem we can convert / set first image of your wordpress post as a featured image. We can achieve this requirement using following two simple SQL Queries.

[code]

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

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

[/code]

For using this particular select queries you need to access your database either through SQL Command Prompt or Front End PHPMyAdmin.

Note: You need to change the prefix of tables in the above select query if you are using prefix any other than “WP”

To explain about the above SQL queries

  1. We are inserting Image Attachment ID for every Post ID into POSTMETA table by fetching these details using simple SQL query which fetches the first image details.
  2. Now, We are updating POSTMETA table to set the inserted image as Featured Image.

 

This above technique will only work for those who attached images through wordpress default uploading feature because we will not have attachment Id for image in any other case. This will not work for those posts that are either imported through blogger or for those posts that you have added images manually.

 

About the author

pavankumar.p1990

4 comments

  • HI Pavan

    Image is really very important part of the post and featured image really gives the great impact.you have rightly given the query, thanks for that.

    I think that is great information shared.

    Sapna

    • Chirpy,
      Just to let you know, It is not always good to use Plugins as it increases the load time of your page and it is advised to use functions in functions.php file than a Plugin. This particular SQL Query lets you set Featured Image without actually touching the code.

      And it is always good to see you here, Keep visiting.

      Regards,
      Pavan.

By pavankumar.p1990