WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an attachment in a post and I want to attach it to another post without detaching it from the first post, using the WP API.

share|improve this question

When inside of the media uploaded in the post edit screen, just search for your previously attached media and embed it in your new post.

The relationship between posts and attachments is preserved in the database. While an attachment can only belong to one post, it can still be embedded in many posts without worry.

share|improve this answer
I don't believe this is true. Media is attached to 1 or 0 posts, not multiple. post_parent contains one value. – GhostToast Jan 15 '14 at 19:51
Yes this is true. I want to do this with wp api. – thanassis Jan 15 '14 at 19:52
That's true, however i assume the user is talking about attaching one piece of media to multiple posts. Which you can do – MichaelJames Jan 15 '14 at 19:54
Not whats reflected in the db. However I see I missed the wp api requirement, since it's not tagged as such – MichaelJames Jan 15 '14 at 19:56
Well, an image or piece of media can be mentioned in any number of posts, but it can only be attached to one post. And what you wrote seems to imply otherwise. "The relationship between posts and attachments is preserved in the database. One attachment can be attached to many posts without worry." – GhostToast Jan 15 '14 at 20:05
up vote 2 down vote accepted

It is not so simple to do this. In my code change post_parent field from the attachment with new post Id.

//take all image-attachments from a post to create post for each 
    $images =& get_children( array (
                        'post_parent' => $event_id,
                        'post_type' => 'attachment',
                        'post_mime_type' => 'image'

            if ( empty($images) ) {
                // no attachments here
            } else { 
                //handle each attachment
                foreach ( $images as $attachment_id => $attachment ) {

                    $this->addPost( $post_id, $attachment_id, $attachment );

function addPost($post_id, $attach_id, $attach)
            // Create post object

            $new_post = array(
            'post_title'    => 'title',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_type'     => 'post'

            // Insert the post into the database
            // create new post that want to reattach the attatchment

            $this->unhookFromSavePost(); // see http://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops
            $new_post_id = wp_insert_post( $new_post ); //get post's id 
            $attach->post_parent = $new_post_id; // post_id 
            $newAddedAttachment = wp_insert_attachment( $attach );

If you want to duplicate the attachment and use it in more posts you have to follow the steps here: wp_insert_attachment.Also, it is posible to copy $attach in new object but you have to unset the ID property of this object.

$new_attach = $attach;
$new_attach->post_parent = $new_post_id;
unset($new_attach[0]);     // unset first property or unset($new_attach[ID]);
wp_insert_attachment( $new_attach);
share|improve this answer

Your Answer


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.