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
- Add custom Attachment Display Setting for images
- Change label of title field for posts in the backend
- Delete the original big size image after upload and leave only 3 images crunched by media gallery
- How do I detach images from posts?
- Exclude drafts in all() view of edit.php
- List latest posts in WP-Admin
- Open WordPress ‘Add New Post’ admin page with parameters set via $_GET
- upload image in a meta box
- How do I use element instead of tags in WordPress post content having webP support?
- upload featured image from front end using wordpress add media button
- Make “Post published. View post” Open in a New Tab
- How to get new post URL?
- Add confirmation popup on “Move to Trash”
- Embed WordPress Admin in an iframe
- Can’t publish, edit or delete posts since Gutenberg update
- Add filter-by-tag in the all posts admin console page
- How to place HTML below the title of the (custom) post overview
- How do I allow users to follow a post and then allow admins to email all users who have followed that post?
- Custom Theme Want To Remove One Category From All Displays
- get custom image size
- How to get only text from wordpress post content?
- Get all images out of the posts content
- how to replace embedded “full” sized images within a post with the “large” ones
- Diplay comment date on WP_Post_Comments_List_Table
- Change default Posts page
- Batch process: remove first image from post content
- How to retrieve the postID in a “image_send_to_editor” hook function?
- I am unable to publish/update post
- Disable inline_edit() on edit.php
- Load post attached images on a single page site with fancybox
- Set size for attachments
- I have a site where attachments in the wp_posts table have guid values of guid, how did this happen?
- Show only posts with images and a fixed amount of posts
- How would one change the default url structure of attachments?
- If statement to check if post has image
- Gather posts into a cart/lightbox and share with another user
- What are the benefits of setting a “featured image” for a WP blog post?
- update image path with words starting uppercase to lowercase chars
- 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
- Make insterted image point to post url instead of attachment page
- how to give multiple post thumbnails to a post
- Change the color of the Password Protected Post titles
- How to allow multiple thumbnail upload for Posts?
- 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 get all inline images in post content? [duplicate]
- Sort All Posts by Category Name in the Admin Panel?
- Inserting Media to WordPress Posts
- Author Avatar as default first image
- Link post images to 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 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
- How to exclude native lazy loading from first image in post content?
- 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
- My page shows some other content in WP
- the wp_post_update isn’t working all the time
- 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
- Modified core/image rendering not showing in posts – only on pages
- Remove status ‘archived’ from the default post and page view
- How to show WP admin edit page within template page/post?
- Blog images not showing on homepage
- Many buttons not functioning in WordPress dashboard (as admin)
- Redirecting to “All Posts” after post publish or update
- Integrate video and iframe in a post type gallery
- how to create other sizes of original image in one go wordpress uploads
- How can I filter the post of a CPT by taxonomy Term in admin meta-box dropdown?
- How to enable comments meta box for other author posts?
- Admin is showing all posts on page 1
- Stop images from being displayed on the front-end of the site
- I moved my site to another server, wp admin works so does the front page, but posts don’t work
- Relative Image Urls in Posts are starting from root – Not from WordPress Address (URL) at General Settings
- WordPress Media URLs?
- Error establishing a database connection
- Assign custom class to post content images
- Post thumbnail is not shown in wp-admin
- Post thumbnail height problem
- Enqueue CSS on custom posts’ backend area
- What does WordPress mean by they have full rights to content [closed]
- Add News Feed to bespoke website, only to one page on whole site
- Post Format Status [closed]
- Broken images after importing posts and opening in Gutenberg
- Search doesn’t display posts with images
- Inside post image title has been posting as a post title on my main post page
- Download button under all images WordPress
- how to display the actual size of the picture in post
- My website freezes for one minute every time I publish or update a post
- Post type gives 404 unless logged in