I have written a plugin that (besides other functionality) makes posts from existing content. For each post I have one picture - how to make them featured programmatically?

I do the:

$id =  wp_insert_post( $my_post );
wp_set_object_terms( $id, $cat_ids, 'category' );

and I would like my next step to be inserting $image (file path or URI) as featured image. How?

Thanks in advance.


Try using set_post_thumbnail().

Assuming you already know how to determine the $post (ID or object) for which to set the featured image, and the $thumbnail_id (ID) of the image that you want to set as the featured image:

set_post_thumbnail( $post, $thumbnail_id );
| improve this answer | |
  • Chip, thanks very much, I've assumed that such function exists, but how do I get $thumbnail_id which is integer, and I've expected file path or URL string? – Miloš Đakonović Nov 5 '12 at 14:21

set_post_thumbnail will allow to set 1 attachment from id to a post of provided id.

If you don't have the attachment id or want to do it directly from url, you will have to create the attachment first, see wp_insert_attachment

you may also upload the image using media_sideload_image

| improve this answer | |

You can use wp_insert_attachment() and wp_generate_attachment_metadata() to make the image an attachment to the post. And you make it a featured image by using set_post_thumbnail(). (it's really just a custom field _thumbnail_id.
Something like:

$attach_id   = wp_insert_attachment( $attachment, $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id,  $attach_data );
set_post_thumbnail( $post_id, $attach_id );
| improve this answer | |

Set Featured images programmatically .

function setFeaturedImages() {

$base = dirname(__FILE__);
$imgfile= $base.DS.'images'.DS.'14'.DS.'Ascot_Anthracite-Grey-1.jpg';
$filename = basename($imgfile);
$upload_file = wp_upload_bits($filename, null, file_get_contents($imgfile));
if (!$upload_file['error']) {
    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_parent' => 0,
        'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
        'post_content' => '',
        'post_status' => 'inherit'
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], 209 );

if (!is_wp_error($attachment_id)) {
    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
    wp_update_attachment_metadata( $attachment_id,  $attachment_data );

set_post_thumbnail( 209, $attachment_id );



Please refer the tutorial for detailed explanation. http://www.pearlbells.co.uk/insert-udpate-wordpress-post-programmatically/

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.