WordPress 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.
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’
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
- 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.
- 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.