How can I set WordPress featured image to a WordPress page programmatically
You can use wp_insert_attachment method to attach a WordPress Image as featured image
$post_id = 1; $url = "http://www.domain.com/logo.jpg"; $http = new WP_Http(); $response = $http->request( $url ); $upload = wp_upload_bits( basename($url), null, $response['body'] ); $file_path = $upload['file']; $file_name = basename( $file_path ); $file_type = wp_check_filetype( $file_name, null ); $attachment_title = sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) ); $wp_upload_dir = wp_upload_dir(); $post_info = array( 'guid'=> $wp_upload_dir['url'] . '/' . $file_name, 'post_mime_type' => $file_type['type'], 'post_title' => $attachment_title, 'post_content' => '', 'post_status' => 'inherit', ); $attach_id = wp_insert_attachment( $post_info, $file_path, $post_id ); require_once( ABSPATH . 'wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path ); wp_update_attachment_metadata( $attach_id, $attach_data );