Moving image attachment from post to another?

How to move image attachment from post to another (not to “Remove Permanently” and Re-Upload) ?

I’m using WordPress 3.5.0 and I have a thousand images to process. Is there a built-in function for this ? or is there a WordPress plugin doing this ?

Solutions Collecting From Web of "Moving image attachment from post to another?"

As far as I know, you’d have to use a combination of built-in functions to achieve this. If there is a plugin, you’d have to research for “Bulk parent” or “Bulk re-attachment”.

Unless you have a logic connecting those posts, I think you’d have to do it post by post. Be it with PHPMyAdmin (changing the post_parent column values for all post_type with attachment value). Be it with an already existent or custom built plugin.

One solution is creating a Meta Box, where you’d enter the target post ID and all attachments from the current post would be moved to the new post.
It can be built researching the following queries:

  • +add_meta_box +save_post +input +text is:answer.
  • +get_children +post_parent is:answer.

There is a very nice and small technique from WPEngineer to add a Re-attach column in the Media Library (wp-admin/upload.php), that may be of interest:

Small Extension for the Media-Library

[…] images can be easily reused. A link will be created so you can access the image directly and there is no need to upload the image again to your new post. Further information can be found in this post.

Saying that images are “attached” to a post has always struck me as misleading. “Attached” implies to me that the images would be associated with a post in some manipulable way. That is the problem. That is not always the case.

Some (many) images are hard coded into the post body. This is what you get when you click the media insert buttons. True, there is a mark in the database that associates the image with the post, but that is just accounting (unless you are using galleries). The HTML for the image is embedded into the post body. While manipulating the database entry may break some things, it does not cause the image to move from post to post. To move those you would need to regex the image out of the post body, find its ID, insert the HTML into another post, and then alter the post_parent of the for the image in the $wpdb->posts table. This is complicated by the fact that there is sometimes an anchor tag or shortcode markup– for example, a [caption]–, or both, around the images.

The thumbnail image is “attached” in the way you would expect (I would expect)– by an entry in the $wpdb->postmeta table under _thumbnail_id. You can simply edit the database to move those images.

Then there is the gallery… Gallery images should probably be movable by altering the post_parent for the images (untested).

If your problem involves the first case, I don’t think there is an easy way to do this, and you better be very good with regex or find a library that will parse your content effectively. The other two cases should be fairly straightforward. You’d just need to Loop though the posts, get the attachments and the thumbnail, and reassign them. I would run that about a 100 times on dummy data before committing it to production though.