Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. $image_id = media_sideload_image($image_url, $post_id, $post_id);
  2. update_post_meta($post_id, '_thumbnail_id', $image_id);
  3.  
  4. function new_attachment($att_id){
  5. // the post this was sideloaded into is the attachments parent!
  6. $p = get_post($att_id);
  7. update_post_meta($p->post_parent,'_thumbnail_id',$att_id);
  8. }
  9.  
  10. // add the function above to catch the attachments creation
  11. add_action('add_attachment','new_attachment');
  12.  
  13. // load the attachment from the URL
  14. media_sideload_image($image_url, $post_id, $post_id);
  15.  
  16. // 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
  17. remove_action('add_attachment','new_attachment');
  18.  
  19. foreach( $posts as $nid=>$post )
  20. $posts[$nid]['ID'] = wp_insert_post( $post );
  21.  
  22. foreach( $posts as $nid=>$post )
  23. foreach( $media[$nid] as $m=>$mitem ) {
  24.  
  25. if( 0 == $m ) add_action( 'add_attachment',
  26. function( $att_id ) use ($posts, $nid, $mitem) {
  27. update_post_meta($posts[$nid]['ID'], '_thumbnail_id', $att_id);
  28. $posts[$nid]['media_urls'][] = $mitem['url'];
  29. }
  30. );
  31. media_sideload_image($mitem['url'], $post['ID']);
  32. remove_all_actions( 'add_attachment' );
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement