product thumbnails for admin list

I've created a form that posts products from the front-end and I'm having trouble getting the images that I post to custom_fields to show up as the featured post thumbnail in the back-end. I'm talking about the thumbnail that's in the very first column of the products list in the back-end (edit) page.

Since it's the featured thumbnail that shows there and my images are all custom_field images now, I really need one of two things to happen. Either the first custom field image (id) gets automatically inserted as the post thumbnail (this would be ideal) or the products list (back-end edit) shows the thumbnail from a custom_field image (post_id) if there is no post thumbnail assigned.

I have tried Auto Post Thumbnails plugin but it's not getting the image (remember it's a post id) from the custom_field and attaching it to the featured thumbnails.

I've also tried putting the full url to an image in a custom_field called "thumbnail" That actually works but is not ideal for a couple of reasons. I need to have the ability to use an upload utility instead of pasting a url and the image size for the product list would have to be exact rather than getting resized like normal.

  • wecreateyou
    • The Crimson Coder

    Here is some code to do what I'm after but it doesn't seem to work with the post id only with an exact url.

    function postmeta_img_update() {
    global $post;
    // If Custom Post Meta Field for a Image/Thumnail Exists
    if( get_post_meta($post->ID, "thumbnail", true) ):
    // Get Image Location and File Extention
    $img = get_post_meta($post->ID, "thumbnail", true);
    $ext = substr(strrchr($img,'.'),1);
    // WordPress Upload Directory to Copy to (must be CHMOD to 777)
    $uploads = wp_upload_dir();
    $copydir = $uploads['path']."/";
    // Code to Copy Image to WordPress Upload Directory (Server Must Support file_get_content/fopen/fputs)
    $data = file_get_contents($img);
    $filetitle = strtolower(str_replace(array(' ', '-', '.', '(', ')', '!', '@', '#', '$', '%', '^', '&', '*', '_', '=', '+'), "-", get_the_title()));
    $file = fopen($copydir . "$filetitle-$post->ID.$ext", "w+");
    fputs($file, $data);
    // Insert Image to WordPress Media Libarby
    $filepath = $uploads['path']."/$filetitle-$post->ID.$ext";
    $wp_filetype = wp_check_filetype(basename($filepath), null );
    $attachment = array(
         'post_mime_type' => $wp_filetype['type'],
         'post_title' => get_the_title(),
         'post_content' => 'Image for '.get_the_title(),
         'post_status' => 'inherit'
    wp_insert_attachment( $attachment, $filepath, $post->ID);
    // Get Attachment ID for Post
    global $wpdb; $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
    // Attached Image as Featured Thumbnail in Post
    update_post_meta($post->ID, "_thumbnail_id", $attachment_id);
    // Removes Custom Meta Field Image URL. This stop this function running again for the updated post.
    delete_post_meta($post->ID, "thumbnail");

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.