Title: Set Thumbnails via Database
Last modified: August 24, 2016

---

# Set Thumbnails via Database

 *  [ColumbiaWebDev](https://wordpress.org/support/users/prfctgdess/)
 * (@prfctgdess)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/set-thumbnails-via-database/)
 * So I have a ton of old posts that have images attached (via the Media Uploader)
   to the post content, but don’t have featured images. OF COURSE, my theme wants
   to use featured images. 🙂
 * I have the post_id of each post in my set. I have both the meta_id value of the
   image for each post and the URL of each image.
 * I want to just do a SQL statement/insert/update in my database backend to set
   all the featured images, but I can’t seem to get it to work. I tried inserting
   the post_id, meta_key=”_thumbnail_id”, and meta_value=my image ID into the _postmeta
   table and … nothing. They don’t show up as attached on the front-end. Am I missing
   a step? Do I need to regenerate thumbnails or something afterward?
 * Any insight is greatly appreciated, thanks!
 * P.S. I know there have been solutions/ideas posted elsewhere about how to accomplish
   this with php or wp hooks, etc. I just want to update from the database side 
   if possible…SO much less confusing for me. 🙂

Viewing 1 replies (of 1 total)

 *  [Chris Huff](https://wordpress.org/support/users/brochris/)
 * (@brochris)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/set-thumbnails-via-database/#post-6037627)
 * So, since no one has offered a SQL solution to this, I thought I may as well 
   offer a PHP solution I use in a plugin I wrote. Just paste it in the functions.
   php file of your theme.
 *     ```
       // Set the first attached image as the featured image, if there isn't one yet
       function set_featured_image($post_id) {
   
           $post = get_post( $post_id );
   
           if (get_the_post_thumbnail($post->ID) != '')
           return;
   
           $images = get_posts(
               array(
                   'post_type'      => 'attachment',
                   'post_mime_type' => 'image',
                   'post_parent'    => $post->ID,
                   'posts_per_page' => 1, /* Save memory, only need one */
                   )
               );
   
           require_once( ABSPATH . 'wp-admin/includes/image.php' );
   
           // Set the first image as the featured image
           if(count($images) > 0){
               set_post_thumbnail( $post->ID, $images[0]->ID );
               }
           }
   
       add_action( 'the_post', 'set_featured_image' );
       ```
   
 * Basically, any time the homepage or a post is visited, it checks to make sure
   there is a featured image set, and if not, sets the first attached image as the
   featured image.
 * Sorry I can’t offer a SQL solution, but this does work.

Viewing 1 replies (of 1 total)

The topic ‘Set Thumbnails via Database’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [featured](https://wordpress.org/support/topic-tag/featured/)
 * [featured image](https://wordpress.org/support/topic-tag/featured-image/)
 * [image](https://wordpress.org/support/topic-tag/image/)
 * [thumbnail](https://wordpress.org/support/topic-tag/thumbnail/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [Chris Huff](https://wordpress.org/support/users/brochris/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/set-thumbnails-via-database/#post-6037627)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
