Did you read doc for set_post_thumbnail()?
You need either post object or post ID for the set_post_thumbnail() function. The function wp_insert_post() will return post id on success. You can use that post id to set the thumbnail image.
Here is the modification for your last couple of lines
$post_id = wp_insert_post( $my_post );
if(!$post_id){
// post insertion was failed. Handle it here
}
//Set Image as thumbnail
set_post_thumbnail($post_id, $attachment_id); //Don't know what to do with this
That should do it.
code not tested
Related Posts:
- Download an image from a webpage to the default uploads folder
- Is there a hook which fires after all thumbnails are generated?
- How can I add the “Use as featured image” to a custom metabox?
- Frontend Post – Allow Only Image File Upload
- Can’t seem to attach uploaded image to post and set it as thumbnail
- How to stop thumbnail generation from some images and different size thumbnail generate
- How to replace images?
- adding image to post thumbnail from another server
- WordPress Multisite: Images moved to /sites/1?
- How to delete original image file after upload and resize
- An error has occurred during the upload process in wordpress version 5.3.2
- Troubles with code for upload a image
- How do I display a PDF thumbnail as a link to the PDF without uploading the image
- Featured Image add tab
- How to change image type for specific size?
- Uploaded images don’t show in Media Library if there are special characters in IPTC Keywords
- Rename files on upload
- Remove title attribute from images
- Insert an image into a post by API
- Saving the pre-sanitized filename of an attachment as the Title or Caption
- Double thumbnails?
- Alter media caption/description conflict in WordPress?
- No srcset for hard-cropped thumbnails
- How to Display an Image Uploaded through a Meta Box in a Template?
- HTTP Error when trying to upload image
- Rename attachments during upload
- Removing Title Tag from Thumbnails
- When Uploading JPEGs, Does WordPress Compress the Original Image?
- Thumbnails produce unwanted gray pixels on white background [closed]
- attach unattached featured images to respective posts
- WordPress reduces the full size image and uses it as the original
- Remove image classes from post thumbnail output
- add_image_size() and uploading the exact size leaves 1×1 image in uploads
- media_sideload_image with rewritten urls?
- How to order images in a post in WordPress 3.3?
- How to set a contact forms total size limit
- SVG Featured image not shown in twitter
- Change WordPress Media Manager
- All Images on wordpress site broken
- WordPress crops images differently on retina screens?
- Multiple images with Media Uploader on front-end
- How to keep WP from recompressing Full Size images
- Alter the image, before save, of a particular image size
- WordPress automatically downscaled images larger that original
- Missing a temporary folder.error when image is uploaded in admin Set temporary Directory – WordPress 5.2.2
- WP fails to upload files to media library, /wp-admin/async-upload.php 403 forbidden
- Add a background image for Custom Post Type archives page
- Images stopped loadding after adding SSl
- Setup Featured image on all posts from thesis thesis_post_image custom field?
- Upload image without media-upload [duplicate]
- Tinymce and uploading local pictures
- Unable to upload images to the Media Library. Getting this erron on the console wp-admin/async-upload.php 403 (Forbidden)
- Create a post from every image in my media gallery
- What can i do add title to ‘large’ image?
- Retrieve Image Attachments Getting Post Thumbnail Image First
- Height and width attribute not setting by the_post_thumbnail
- How to set minimum required size for featured image?
- All Images not calling alt text
- Can we use featured image as third party website image URL
- Show (print) featured image all dimensions (height, width)
- Serve different images per screen size and cache possible?
- Product Images Low Quality After Updating From WP 3.5 to 4 [closed]
- Convert featured images into “product images”
- Resize uploaded images [duplicate]
- How to use the thumbnail size with catch_that_image
- Making a parent directory writable by the server
- Indicate image sizes for upload by post type
- How to display a warning when an uploaded image is too small?
- my featured image of wordpress are not showing in the front end though image are uploaded in media library
- Large uploaded images not resizing?
- How to change Gallery image url in product page ? In function.php?
- Form File Upload Field generates multiple copies of Image Files (different sizes): Is this a WP feature or is this being done by the Plugin
- Upload error on localhost (at minimum, not yet tested online)
- Custom image size not displaying with wp_get_attachment_image()
- Unset image sizes before images are uploaded
- unsharp thumbnails after uploading image
- An error has occurred during the upload process in wordpress 5.3.2
- Show featured image in all posts
- Custom Background for each page and post
- How to handle a lot of media files in the uploads folder? (+10.000)
- Using custom field to change alt text for a featured image
- Getting the Select and Crop Functionality Using Custom Fields
- Get custom size of thumbnails
- After I upload a file to library its name converted to dashes
- Reduce Mobile Image upload size (wordpress)
- Only User Upload image Http Error
- See the all uploaded images of a user as an admin
- Broken Media Library
- Utilise WP_Customize_Image_Control to only show image picker
- 3.5.x Theme Customize: Handling Custom Control Requests
- Custom filename when pasting an image from clipboard
- Best Photoshop-WordPress workflow for editing images without re-upload [closed]
- How to import external media items into wordpress media library (without using storage)
- Generate and upload screenshot as featured image for user-submitted post
- Can’t upload svg files in WordPress
- Random images from uploads folder and subfolder
- Trying WP editor to load full size image (not ‘300×300’)
- How to get rid of a blank space where the featured image used to be?
- Upload media file failed. Uploadtest failed
- Restricting Image Upload Sizes using ‘wp_handle_upload_prefilter’ – Stuck media progress bar when Featured Image?