Upload image and assign it’s id to post

WordPress attachments are saved in the wp_posts table with the post_type field set to 'attachment'. The post id for the post that the attachment is ‘linked’ to is in the post_parent field. Keep in mind that an attachment being ‘linked to a post is not really a solid connection. It is very possible to have an attachment linked to a post but not actually embedded into that post, and equally possible for the same attachment to actually be embedded into a completely different post. (Lets just call it an open relationship.)

Conversions are difficult, but if you adding the post programatically and you have the post id for it, if you are creating the attachment using wp_insert_post(), you can specify post_parent in your post array:

'post_parent'    => [ <post ID> ] //Sets the parent of the new post.