Set (featured) thumbnail for post?

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.

Leave a Comment