好的,我通过破解另一个插件找到了答案。
首先,我遍历 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!