Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $image_id = media_sideload_image($image_url, $post_id, $post_id);
- update_post_meta($post_id, '_thumbnail_id', $image_id);
- function new_attachment($att_id){
- // the post this was sideloaded into is the attachments parent!
- $p = get_post($att_id);
- update_post_meta($p->post_parent,'_thumbnail_id',$att_id);
- }
- // add the function above to catch the attachments creation
- add_action('add_attachment','new_attachment');
- // load the attachment from the URL
- media_sideload_image($image_url, $post_id, $post_id);
- // we have the Image now, and the function above will have fired too setting the thumbnail ID in the process, so lets remove the hook so we don't cause any more trouble
- remove_action('add_attachment','new_attachment');
- foreach( $posts as $nid=>$post )
- $posts[$nid]['ID'] = wp_insert_post( $post );
- foreach( $posts as $nid=>$post )
- foreach( $media[$nid] as $m=>$mitem ) {
- if( 0 == $m ) add_action( 'add_attachment',
- function( $att_id ) use ($posts, $nid, $mitem) {
- update_post_meta($posts[$nid]['ID'], '_thumbnail_id', $att_id);
- $posts[$nid]['media_urls'][] = $mitem['url'];
- }
- );
- media_sideload_image($mitem['url'], $post['ID']);
- remove_all_actions( 'add_attachment' );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement