Perhaps use set_post_thumbnail()
? (Codex ref.)
EDIT
To get the attachment ID using the Post ID:
// Associative array of attachments, as $attachment_id => $attachment
$attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
$attachment = $attachments[0]; // ID of your single, attached image.
Then to set it as the featured image:
set_post_thumbnail( $attachment );
I have to check to be sure; the docs are a bit confusing. The default output might be an object. Regardless, get_children()
will get you to the ID of your lone attachment, given the Post ID.
Related Posts:
- How to get image title/alt attribute?
- How to Fix HTTP Error When Uploading Images?
- Delete original image – keep thumbnail?
- Set Featured Image Front Frontend Form
- media_sideload_image generates blank?
- How to change _wp_attachment_metadata specific value in SQL?
- How can I get a different image size, if all I have is the link?
- Delete all thumbnails for a post
- How to set an attachments parent post id in code?
- How do i show wordpress attachments from current post?
- Need clear explanation about medias/files/attchments
- Image still attached to post even after removing it as featured image
- Exclude featured image from attachment loop
- How to create resized versions of images added with wp_insert_attachment
- Show Multiple Size URL of one Featured Image/Thumbnail?
- Check if parent post is sticky on attachment page?
- How to attach images that I “insert from url” into the post?
- Moved server, and WP now uses original images for all thumb sizes?
- replace featured image from frontend
- Featured Image URL is Showing but not thumbnail
- Import Dummy Content and Post Thumbnails
- Get all images from posts with maximum number and without featured image
- Why not attaching images after impoting using wp cli
- Close featured image modal windows with JS
- Get the first image from post content (eg.: hotlinked images)
- How do I regenerate thumbnails?
- Add Title Attribute to WordPress Image the_post_thumbnail
- Replacing Gutenberg Featured Image control?
- How to find attachment by it’s name?
- oEmbed, thumbnails and wordpress
- the_post_thumbnail scaling not hard cropping
- Featured Image Inherited from Parent Page
- Change wording of default thumbnail metabox
- Specific featured image for each category
- how to pull images with no add_theme_support(‘post_thumbnails’)
- Can WordPress crop only the post thumbnails?
- Query the post which have a thumbnail
- Adding schema itemprop image to the_post_thumbnail with filters
- Show prev and next post links for parent post of current image in attachment page?
- wordpress responsive image srcset not working
- How do I get a random image from subset of images in media gallery?
- Why are extra 92 pixel square thumbnails created?
- wp-cli regenerate media is not working for custom sizes
- Remove dimensions from filename featured image
- Why does the Hard Crop option Scale?
- Post Thumbnails multiple sizes
- Long Post Title Misaligning Grid with Featured Image
- How to add multiple featured images to post/page without a plugin
- How do I set “Upload Image” as detault “Set Featured Image” window?
- Featured Image won’t display on Posts page
- Getting images to show up in RSS Reader
- how i can increase Featured thumbnails size?
- the_post_thumbnail() Custom sizes not display correctly
- Set_post_thumbnail not resizing images
- MultiPostThumbnails for Custom Post Type
- Muliple featured images (or just images) per post
- WordPress Featured Image function recreate
- Using Featured Image as Hero Background
- wp_get_attachment_image_src match smallest edge
- Can’t Set Featured Image [closed]
- Resize post thumbnail produces horrible image quality..Any help?
- media-library: how to insert thumbnails with a fixed width, regardless of landscape/portrait orientation
- Post thumbnail outside of loop
- query_posts adding two “post-entry” divs
- Display a thumbnail outside of the loop for a page
- How to use get_the_title with text on the ‘alt’ of the_thumbnail array?
- remove_image_size doesn’t seem to be working
- Get Featured Image Outside Loop Not Working
- Responsive scaling of image for the_post_thumbnail() [closed]
- images not showing despite using add_theme_support(‘post-thumbnails’);
- Retrieve Image Attachments Getting Post Thumbnail Image First
- post thumbnail: increase dimension if smaller than… Possible?
- Displaying caption with featured image
- How risize a thumbnail?
- custom thumbnail image size not working
- Comparison Operators not working for conditional author post thumbnails
- Assign SQL ‘post_thumbnail’ column as featured image
- Default thumb – how to set it
- How to update custom image sizes?
- featured images twenty eleven theme
- Picture inserted in the post not showing in “Gallery”
- featured image is not showing
- Custom image size not displaying with wp_get_attachment_image()
- How to change the format of thumbnails into wordpress theme?
- How to calculate the sum of all featured thumbnail height on WordPress?
- How to create thumbnails programmatically?
- Usage of add_image_size (for featured image)
- Featured image: Dynamic image sizes when enlarged with javascript
- Smaller Size for featured images
- Featured image doesn’t appear in Blog index
- Remove Featured Image based on Date
- How to show only 2nd featured img on home/blog
- Echo recent posts each with its own respective featured image
- How to get post thumbnail to be equal to set media size
- Mass/Bulk assign featured images to posts
- setting featured image is not working while running script in background, works when running that script from browser
- How to modify responsive images in post thumbnails
- get_the_post_thumbnail() – Invalid argument supplied for foreach() in /wp-includes/post-thumbnail-template.php on line 64
- Where is the template from the_post_thumbnail()?
- Get Post Thumbnail Outside of Loop