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
- Each custom image size in custom upload directory?
- How to automatically add rounded corners to thumbnails?
- Rename files on upload
- Remove title attribute from images
- How to limit number of images being printer out in “Set Featured Image” pop up?
- How to copy and paste a picture found on the web to wordpress easily
- Get attachment ID of author_meta image – Attachment Metadata
- Black and White thumbnails
- How to Display an Image Uploaded through a Meta Box in a Template?
- Download button for Featured Image in every post – automatically
- Removing Title Tag from Thumbnails
- When Uploading JPEGs, Does WordPress Compress the Original Image?
- Uploading images: ‘ø’ get replaced with ‘ø’
- SSL: How to make customizer images Protocol Relative in WordPress?
- Insert image in comment reply
- attach unattached featured images to respective posts
- Thumbnails are bigger in size than the original image
- 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?
- Featured images not displaying at full resolution
- Faster way to put images into a blog post by using Add Media dialog
- Handling image uploads without thickbox
- Alter the image, before save, of a particular image size
- Loose images in uploads folder
- Force minimum image size and aspect ratio when uploading images
- 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
- Convert uploaded GIF to non animated image automatically
- Is it possible to split Default WP Media directory to multiple Folder/Subdomain?
- Ho to add “Create Slider” option to default “Add Media” popup?
- Images stopped loadding after adding SSl
- Images in Media aren’t showing, not broken
- Fixing image names for a site being restored
- Upload Image using wp_insert_attachment and file url don’t have file extension
- Images uploaded to the media library has strange colors. However, in the edit screen the color is ok
- Setup Featured image on all posts from thesis thesis_post_image custom field?
- Export xml use/import images from theme directory
- Original image not saving?
- Get custom thumbnail sizes [duplicate]
- How to get the Gallery form/section just like in Gutenberg block?
- Custom responsive ‘featured image’ sizes
- the_post_thumbnail(‘medium’) setting some images width=1 height=1
- How do you import images from a URL in your post?
- Get images attached to a specific page
- add_image_size function not working
- What sense makes medium_large_size_h => 0?
- How do I link a featured photo to it’s post?
- Is it possible to enable the ‘Link To’ field under ‘Attachment Display Settings’ for a Featured Image?
- after migration, newly uploaded images won’t show up
- Use default image as var
- Image upload error?
- How to add “alt” attribute for image during upload at WP front-end?
- How to use getimagesize() of image upload?
- How to change image’s author via a function when using GravityForms uploader? [closed]
- Alter the image used for a user who has not uploaded a custom image yet
- Allow users to upload multiple images to their profile
- Using different size thumbnails on different parts of my site… with functions.php?
- attach several images to post + gallery
- Serving Images from subdomain in wordpress
- User uploads image as “Featured Image” but WordPress resizes it?
- How to increase media image scale limit?
- WordPress PHP custom function is causing 500 Internal Server Error Connection Timeout
- Can’t upload Images to WordPress – Error 500 Response
- Force WordPress to always generate a thumbnail for original image
- Get post thumbnail in specific size
- WordPress 4.4.2 upload give error, can’t write is upper directory allowed to be written
- How to trigger “wp_handle_upload_prefilter” filter when uploading an image programatically?
- featured image in custom poste type (backend)
- Broken images in media library
- Run command to optimize images after they get uploaded to WordPress
- Where to add my code to auto attach images to existing products
- Declare global variable that can be used on other function
- Set WordPress to Create different images format later, not during upload
- The most of the images don’t appear, although URLs are correct
- Add two images with Sweet Testimonials
- Black and White Thumbnail in WordPress Gallery
- Upload button in metabox not working
- Import existing image as a single post
- Upload unnattached image from frontend
- How do I change a Themes Header Image?
- Media Library is not loading on grid view in admin panel
- Blurry Images WordPress 5.8.1
- Media upload error
- What does WordPress media uploader do to image files on upload?
- How to get WordPress to resize images for srcset?
- WordPress pointing animated GIF images with wrong file name upon upload (GIFs animation not working)
- Edit multiple posts featured image