// Set video generated thumbnail as featured image***

if(isset($posted_data['video-file']) && $posted_data['video-file'] != ''){
              //Wait 10 seconds for thumbnail to be generated   
              //  $file_location = '';
                    $file_location = $posted_data['video-file'];
                } else {
                    $file_name = $upload_dir['basedir'] . '/' .$filename."_thumb1.jpg";
                    $uploaded_files = $contact_form_7->uploaded_files();
                    $file_location = $upload_dir['basedir'] . '/' .$filename."_thumb1.jpg";

                $res2 = set_post_thumbnail( $new_ID, $file_name);

  • Sorry this posted all wrong. I'll try this again.
    – Marcus
    Mar 18, 2019 at 0:14

1 Answer 1


set_post_thumbnail() requires the second parameter to be an int for attachment id, so the statement $res2 = set_post_thumbnail( $new_ID, $file_name); does not work.

First you need to add the image to your database as an attachment and then you will be able to use its id to set it as thumbnail.

See this example for more help and detail to set an image as featured image.

  • 1
    Your correct. The auto generated thumbnail preview is created by ffmpeg which is done when the video is uploaded. It adds this image into the media library which I am trying to retrieve to set it as the featured image on my video blog post. Because this image is already in the library, I am trying to figure how to get its "post id" to use as the "attachment id" and use the "$file_name" as its file path.
    – Marcus
    Mar 18, 2019 at 9:43
  • You get the attachment_id when image is added to media library using wp_insert_attachment(). See the updated answer. Mar 18, 2019 at 10:54
  • Makes a lot of sense. So if I have my video file that is IMG_0676.mp4 and the auto generated preview from that file is IMG_0676_thumb1.jpg with a Post_ID of XXX which is already in the media library, to get this file should I use wp_get_attachment_image_url(Post_ID (which would be the new attachment_ID) ) as the $file_location?
    – Marcus
    Mar 18, 2019 at 17:22
  • Can you share te code where the generated thumbnail is inserted into media library. This will help in explaining the issue. thanks. Mar 18, 2019 at 17:39
  • 1
    That did it. Thank you Qaisar. You are awesome.
    – Marcus
    Mar 19, 2019 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.