わかりました、別のプラグインをハッキングして答えを見つけました。
まず、postmeta テーブルをループします
$postid_list = $wpdb->get_results("SELECT distinct post_id FROM yars_postmeta WHERE meta_key='featured_image' ORDER BY post_id DESC LIMIT 10");
if (!$postid_list){
die('No posts with images were found.');
}
foreach ($postid_list as $v) {
$post_id = $v->post_id;
//$options['url_method'] = $url_method;
echo fig_fetch_images($post_id).'<br/>';
}
次に、関数で画像を取得してメディアライブラリにアップロードし、投稿IDの注目の画像を設定します
function fig_fetch_images( $post_id ) {
global $wpdb;
//Check to make sure function is not executed more than once on save
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if ( !current_user_can('edit_post', $post_id) )
return;
remove_action('publish_post', 'fetch_images');
//$post = get_post($post_id);
$first_image = '';
$key = 'featured_image';
$first_image = get_post_meta($post_id, $key, true);
$wpdb->query("update yars_postmeta set meta_key ='featured_image_uploaded'WHERE meta_key='featured_image' AND post_id=".$post_id);
if (strpos($first_image,$_SERVER['HTTP_HOST'])===false) {
//Fetch and Store the Image
$get = wp_remote_get( $first_image );
$type = wp_remote_retrieve_header( $get, 'content-type' );
$mirror = wp_upload_bits(rawurldecode(basename( $first_image )), '', wp_remote_retrieve_body( $get ) );
//Attachment options
$attachment = array(
'post_title'=> basename( $first_image ),
'post_mime_type' => $type
);
// Add the image to your media library and set as featured image
$attach_id = wp_insert_attachment( $attachment, $mirror['file'], $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $first_image );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
// re-hook this function
add_action('publish_post', 'fetch_images');
}
return ('Done post '. $post_id .' : '. $first_image);
}
元のプラグインは Hotlink Image Cacher でした。