Function Reference/wp insert attachment
$image_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
This function will return an image id which you can use for the featured image.
update_post_meta($post_id, "_thumbnail_id", $image_id);
Related Posts:
- How to upload imagick resource to media in wordpress
- How do I get images (with a thumbnail preview) to show in search results?
- WordPress automatically adds links to uploaded images
- Get featured image with custom size outside WordPress
- How to update custom fields using the wp_insert_post() function?
- How to make a image-size selected by default in Media upload – WP v3.5
- What’s the best way to use the Featured Image for responsive web design?
- Uploading media with the REST API
- Remove “Insert from URL” link in Media upload – WP 3.5
- Adding a custom image upload size and making it selected by default?
- How to draw media details for Isotope gallery using Photoswipe
- Hide old attachments from wp media library
- Failed media upload: “The uploaded file was only partially uploaded.”
- Adding Featured Image to Post programatically
- How to use SRCSET with get_the_post_thumbnail()?
- Adding Image Count to Multigallery
- Get a Page’s attachments in Gallery order
- WordPress media upload “HTTP error”
- Using an Image Slider twice on the same page
- How to get Poster (thumbnail) Image of Video
- Assign a picture URL to a page via PHP
- How to scale image with equal width and height without distortion?
- Featured image fallback link to permalink
- is it possible to force wordpress to always save thumbnails as ‘jpg’ not ‘png’
- How to Display Image Meta underneath EVERY image in EVERY post
- How can I show the post thumbnail from the most recent of a certain post type, in widget?
- wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated
- How to get media objects
- How do you use WP slugs for PDF files / media?
- Can’t get wp_insert_post to work
- Randomly Assign an Image’s Alt Text Based on Data From Post
- Showing a different gallery in a seperate post
- Create own WordPress shortcode gallery
- Media Gallery Upload photo incorrect way like glitch
- Is it possible to use Data URI image to set a post thumbnail?
- WP Post-Thumbnail
- Auto-Select Parent Category as Primary
- Image rotation and editing stopped working in WordPress after upgrading Ubuntu from 16.04 to 18.04
- Don’t insert if permalink is the same?
- Upload multiple images to a Woocomerce product
- Display custom post type title as text if post thumbnail is not available
- What is the alternative to “ when it comes to calling Media (image) files in the ‘attachment.php’ file?
- Use of wp_insert_post and parameters
- Getting error when using wp_insert_post()
- Way to add captions to gallery images by code
- Custom gallery in wp3.5
- height should be set to auto to avoid pixelation in the post thumbnail function
- Returning data instead of echoing/printing
- Display related posts without a plugin
- WordPress import media error
- Apply class to every third list item? [duplicate]
- Timthumb.php image gallery not working on Multisite WordPress
- WordPress get_media_embedded
- wp_insert_post() with HTML tags using PHP
- Change default gallery images – Currently they are displaying full size imagese
- get post id from wp_insert_post for get_template_part
- Help Fallback (thumbnail)
- How to add post thumbnail dynamically using Jquery?
- How do I increase the upload size only when editing special pages?
- How to set a featured image (thumbnail) with PHP?
- How to change thumbnail src attribute?
- Error display post thumbnails for previous and next post
- Get the featured image url of clicked post
- Fallback image URL if no featured image exists via a shortcode function
- How to use `wp_insert_post()` and `get_permalink()` functions?
- How to pick “full/thumbnail” images in the loop?
- How to Make Thumbnail of Post Stay Animated
- Get the alt text of the image in a array
- Convert all uploaded PNG files to PNG-8 format
- How to decrease the max file upload size without using php.ini or htaccess?
- Hook on file upload
- Create shortcode for metabox gallery
- wp_query – Exclude the first thumbnail from lazy loading on archives
- How can I pass php code as a content while publishing a post
- Replace background image with post thumbnail in shortcode
- How to add WordPress Featured image in tag ..?
- ACF Gallery field images donwload
- add custom link to gallery images
- How to programmatically set existing image in media library the featured image for a post in wordpress
- Meta inserted through wp_insert_post gets messed up
- How to link featured images in single.php for only non-logged-in users?
- Thumbnail wouldn’t scale to size properly
- how to call other plugins once custom post has been inserted
- wp_delete_attachment not working with multiple values
- Get posts associated with media attachment
- Using catch_that_image() for galleries?
- Can’t put a hyperlink on Featured Post’s Image
- Front end post submissions do not get submitted in the category
- News and Featured image
- How to correctly add the featured immage (thumbnails) support in a WP theme? Why I can’t add a featured immage?
- Creating image gallery in wordpress
- Overwriting a Shortcode
- Post Thumbnail on Single (if elseif else)
- show/hide attachments
- the_post_thumbnail unless video id is added
- Can’t upload files
- Edit Image/Image Details – Replace button missing
- add if statement to the featured image
- How to automatically convert images to WebP on WordPress?
- PHP – Multiple variables in insert post array