Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm currently working on importing an MovableType blog into WordPress. The blog has several thousand posts, and each post has an image associated with it.

The export file contains references to each image as follows (and as expected):

<img src="http://domain/path/to/image"> 

I've got all the images, and have rewritten all the links in the export file to use the new (local) location. However, the WordPress import process doesn't actually create attachments for the post, which I need.

So - what's the best way of doing this - right now I'm thinking about iterating through the set of posts, parsing out any image urls, and creating new posts:

  • post_parent = the actual post
  • post_type = attachment

Questions

  • Is post_guid the image location reference? Or is the path to the image stored somewhere else?

  • Where is featured image set?

  • Is this the best way of doing this?

[I've tried using the cache-image plugin, but it borks on the number of posts / images]

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Answers:

Is post_guid the image location reference? Or is the path to the image stored somewhere else?

$post->guid is the record in a post which holds the URL for your attachment.

Where is featured image set?

featured image is saved as post meta so use update_post_meta() once you have the attachment id:

update_post_meta( $post->ID, '_thumbnail_id', $attachment_id );

Is this the best way of doing this?

Its a lot of work for several thousand posts and any way you choose will cause you some "pain" but it seems like a reasonable way of accomplishing this.

share|improve this answer
2  
note for myself that wp_insert_attachment() is better than trying to add field data manually –  anu Apr 21 '11 at 15:25
    
@anu: :) that is so true!!! :) –  Bainternet Apr 21 '11 at 15:30
    
even better - media_handle_upload() - great post here about how to use it from @goldenapples –  anu Apr 21 '11 at 15:30
    
pretty late, but this can be also helpful: wpscale.com/… –  Ionut Staicu Sep 3 '12 at 6:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.