To set a featured image when creating a post using wp_insert_post()
, you can use the set_post_thumbnail()
function
// Create the post
$post_id = wp_insert_post($args);
// Check if the post was created successfully
if (!is_wp_error($post_id)) {
// Set the featured image
if ($image_url) { // $image_url should be the URL of the image you want to set as the featured image
$image_id = media_sideload_image($image_url, $post_id, '', 'id'); // Upload and attach the image
if (!is_wp_error($image_id)) {
set_post_thumbnail($post_id, $image_id); // Set the uploaded image as the featured image
}
}
}
Related Posts:
- Set featured image on creating new post
- What are the benefits of setting a “featured image” for a WP blog post?
- preg_replace not removed “class”
- update image path with words starting uppercase to lowercase chars
- Fast publishing in wordpress
- How to only publish posts with image in it
- Which file do I need to edit the All Posts page in the admin area?
- Uploading images in custom post form
- Upload attachment from external site
- Make insterted image point to post url instead of attachment page
- How do you search for a post or page with a certain url on the WordPress admin panel?
- Get URL from all images in a post
- Disable posts generated from image uploads
- How to get the old WordPress Draft Editor view back with the Word count?
- how to give multiple post thumbnails to a post
- Change the color of the Password Protected Post titles
- Posts page not showing correct image
- Images showing up in Post Editor, but not in Published Post
- How to allow multiple thumbnail upload for Posts?
- Image behind the post’s title
- get_children returns older images
- How to display post title inside thumbnail?
- Bolding several words at once
- How to remove text and retain in caption image using the_content function
- How to retrieve certain number of images from a wordpress post?
- Show more than 200 images in one post
- How to get all inline images in post content? [duplicate]
- Hide thumbnail if there is a gallery on the post
- Image on rss feed on post
- Change the background of each post according to the category
- Sort All Posts by Category Name in the Admin Panel?
- How to Featured content with images in home page?
- Where to insert the other site image url for our wordpress post
- select post preview image from nextgen gallery
- How to show a featured image inside a post in only 1 single category
- Inserting Media to WordPress Posts
- Author Avatar as default first image
- List latest posts with least comments in WP-Admin
- Lightbox on wordpress post page
- Link post images to post
- How to get the next and previous image title from attachment or gallery?
- Attachment image single page
- How to change the order of elements within a post?
- Only show featured image on exerpt and exclude images in post
- WordPress as CMS: How to manage/handle images assigned to a post as full-sized background images in a slideshow?
- How to call WordPress First Image Post or else Attached Image or Default
- Regarding Posts
- how can display a post on home screen without images
- Some blog posts containing source code fail to save draft or publish
- Changing The Default Header POST Title and Description text by category in wordpress
- Site/Post preview is not working on all social media
- How to exclude native lazy loading from first image in post content?
- WP_Query outputs wrong post in custom post type
- Edit post & page option does not display on latest wordpress
- Cannot create new posts or upload images using media manager
- Rewrite image links from attachment id to attachment link
- user_has_cap filter allows “edit_others_posts” but not is not allowing updating/publishing
- ‘QTags.addButton: arg2 distorts the button rather than just shows
- Delete media from the post but keep my featured image and post
- Custom Fields – How to get the list of a specific active widget each time it rendered
- Aligning images in the wp editor not working. How do i get this to work in custom theme?
- unable to write to the database while uploading images
- My page shows some other content in WP
- the wp_post_update isn’t working all the time
- Redirect old Posts URL to new URL
- Pinterest button shows image options from different posts
- Change the size of the featured image for post?
- Post count doesn’t match number of posts shown in list in admin
- How to view a post from the wordpress admin screen
- Display image gallery if more than 1 image
- How do I fix broken links to images in imported posts’ post_content?
- Blog open in new Tab
- Thumbnails and images in the blog post are not loading
- Modified core/image rendering not showing in posts – only on pages
- Remove status ‘archived’ from the default post and page view
- Strange behaviour in WordPress admin
- Post images are not displayed in media library
- How to show WP admin edit page within template page/post?
- WordPress Screen Options, Help, Visual editor, Status, Visibility, Revisions, Published on and any toggled indicator don’t work for posts
- Delete attached images from posts
- Blog featured images disappeared on my homepage
- Blog images not showing on homepage
- Center crop the feature image
- Many buttons not functioning in WordPress dashboard (as admin)
- Redirecting to “All Posts” after post publish or update
- Schedule image post on download
- Add another feature image box
- ‘posts’ table should not store the absolute image path
- How to show category image if no featured image is set?
- How to make an image appear as a thumbnail in the blog post?
- Integrate video and iframe in a post type gallery
- Manipulating images inside post content
- Blank page after changing number of post displayed in wp-admin
- How to sort posts in admin by name
- how to create other sizes of original image in one go wordpress uploads
- Output wp_link_pages as raw url
- How can I alternate styling of images in a post? [closed]
- Is it possible to display QUICK EDIT as default for all posts in wp-admin/edit.php page?
- How can I filter the post of a CPT by taxonomy Term in admin meta-box dropdown?
- issue in wordpress auto delete post and redirect to another 1