What’s the database relationship between a post and its attachments

I’m creating a custom import script to migrate CSV data into wordpress. Each post has a series of attachments that must come with it.

Following chapter 14 (importing via a custom mysql script) in the book WordPress Professional Development (1st edition), Everything is importing fine, except the book does not explain how to get attachments into posts.

I can see my custom posts in wp_posts, and I can see the attachment post_type. But I can’t see how the two are related.

I’m assuming that I’m going to have to save the post first, get the id, and then use that somehow in a relationship with the attachment.

What would be the workflow here (if that’s incorrect)? Can you provide an example insert statement(s) that demonstrate how to use sql to get a post into WordPress?

1 Answer
1

The post that an attachment is related to is determined by the post_parent column on the wp_posts table (depending on your database prefix). Set it to the post_ID of the parent post.

Leave a Comment