• Resolved forexfilms.com

    (@forexfilmscom)


    I’ve come to a brick wall trying to set the featured image of a post with PHP. I’m using wp_insert_post() to make the post but I don’t see any option to set the featured image. I also can’t seem to find a function to set it.

    What I specifically want to do was add a featured image from a url. The theme I’m using supports featured images and I’m currently manually entering them into posts.

Viewing 1 replies (of 1 total)
  • Thread Starter forexfilms.com

    (@forexfilmscom)

    $wp_filetype = wp_check_filetype($filename, null);
    $attachment = array(
    	'post_mime_type' => $wp_filetype['type'],
    	'post_title' => $filename,
    	'post_content' => '',
    	'post_status' => 'inherit'
    );
    
    $attach_id = wp_insert_attachment( $attachment, $thumbnail, $post_id );
    // you must first include the image.php file
    // for the function wp_generate_attachment_metadata() to work
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $thumbnail );
    wp_update_attachment_metadata( $attach_id, $attach_data );
    
    // add featured image to post
    add_post_meta($post_id, '_thumbnail_id', $attach_id);
Viewing 1 replies (of 1 total)

The topic ‘Use PHP to set Featured Image’ is closed to new replies.