Add image attachment to post via coding?

I tried to add an image as Attachment image to a post.
in wp_postmeta i added _wp_attached_file as meta_key and value = productimages/Routers/v/CISCO1941.jpg
it not works and i could not see the image as attachment! but when i use add image button in wordpress’s HTML editor, it add exactly this field to that table but with a different post ID!

is there any dependency for that field?
screenshot from db: http://i.stack.imgur.com/mVlse.png

2 Answers
2

You do not add image attachment to post. In WordPress image attachment is also post. See post types in Codex.

  • what is usually called post on surface is technically post of post post type.

  • image attachment is a post of attachment post type.

So what you need to do is to create image attachment (using wp_insert_attachment() and couple more related functions) and set your post as parent for it.

Leave a Comment