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 manually fix the WordPress gallery code using PHP in functions.php?
- How to make a image-size selected by default in Media upload – WP v3.5
- How do I change/modify the_post_thumbnail(); html output?
- 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
- Decrease file size upload in Media
- Adding a custom image upload size and making it selected by default?
- Set media upload attachment link to none and hide it in WP v3.5
- 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
- Add custom image sizes to media uploader
- How to add a featured image to a existing post via php?
- How to use SRCSET with get_the_post_thumbnail()?
- Upload images and attachments from frontend form
- Set Primary category using the Yoast SEO plugin
- Adding Image Count to Multigallery
- Get a Page’s attachments in Gallery order
- WordPress media upload “HTTP error”
- Adding featured image via PHP
- WordPress loop specific thumbnail size
- Using an Image Slider twice on the same page
- fallback image for featured image
- How to get Poster (thumbnail) Image of Video
- WP Insert Post function – Insert Post Thumbnail
- Display All Post Attachments and Assign Class to the Last Image?
- How to return output of the new wp gallery block for automatically Link images To Media file
- How to make thumbnail image fit into a div where image dimentions are completely different?
- 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 not working
- wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated
- How can I add pagination and how can I change thumbnail size?
- How to get media objects
- Echo URL of large version of Featured Image
- How to stop featured image thumbnail [set as background image] on blog index page just repeating same image across all posts
- Add a class to the anchor tag on HTML5 gallery
- Trying to add attribute to my posts’ featured image
- How do you use WP slugs for PDF files / media?
- Can’t get wp_insert_post to work
- Recent posts with featured image or fallback image with permalink
- Randomly Assign an Image’s Alt Text Based on Data From Post
- WP dynamic featured image – Can’t get second featured image url
- Set the background to a default image if there isn’t a specified “featured image”
- Showing a different gallery in a seperate post
- Create own WordPress shortcode gallery
- PHP variable = get_the_post_thumbnail_url outputting without slashes
- 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
- How to get next image with this code from same post?
- 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?
- Upload featured image from front end using media-upload.php
- How do I get around “Sorry, this file type is not permitted for security reasons”?
- 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
- post thumbnail, conditions, else wont work
- ‘Post-thumbnails’ feature does not seem to register
- height should be set to auto to avoid pixelation in the post thumbnail function
- Three Questions with Twenty Eleven theme
- Returning data instead of echoing/printing
- Display related posts without a plugin
- WordPress function and string as variable?
- Resize image to specific dimension (X to Y ratio)
- Get title attribute from galleries
- WordPress import media error
- Detecting if parent page has gallery
- Apply class to every third list item? [duplicate]
- WordPress gallery shortag
- Timthumb.php image gallery not working on Multisite WordPress
- WordPress get_media_embedded
- Automatically add images to a menu
- wp_insert_post() with HTML tags using PHP
- Using featured image of blog archive page
- Change default gallery images – Currently they are displaying full size imagese
- if custom field doesn’t exist, use post thumbnail instead, as image background
- Add new post in existing categories using wp_insert_post
- get post id from wp_insert_post for get_template_part
- Help Fallback (thumbnail)
- Returning all values from foreach in function php [closed]
- Generate CSV file and add data as a new wordpress custom post
- How to add post thumbnail dynamically using Jquery?