I'm trying to associate an existing image from the media library with a post (It's a custom post type), If I use wp_insert_attachment I get a duplicate in the media library, so I'm getting the image id and using that as the attach_id.

$attach_id = intval( $file['id'] );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
$result = wp_update_attachment_metadata( $attach_id,  $attach_data );

This doesn't work and the $result is false.

I'm sure I'm missing something simple. Can anybody enlighten me?

  • are you want to set featured image? – Anjum Sep 9 '13 at 17:34
  • No, I want to associate a number of images with a post. like a gallery of images for that post, I'm using the built in media dialog to upload images but I need to associate existing images selected using the new media dialog. Thanks. – Lee Sep 9 '13 at 22:13
  • How are you planning to display the attachments to the user? – Cyrcle Sep 10 '13 at 3:57

You can modify an existing media library image using wp_insert_attachment by setting the ID key in the parameter array.

$attachment = array(
    'ID' => $existing_library_attachment_id,
    'post_parent' => $custom_post_id
wp_insert_attachment( $attachment );

This will update the attachment post with ID $existing_library_attachment_id to have a post_parent value of $custom_post_id. However, the only thing this will affect is the permalink of the attachment post. Unless you do something else to edit your custom post itself, you won't see any changes on that post.

Here are some things you can do to attach the image to your post:

  1. To make the image the "featured image" of your post, use the set_post_thumbnail function:

    set_post_thumbnail( $custom_post_id, $existing_library_attachment_id );
  2. To show the picture in the content area of your post, you need to edit the post_content of your post (with wp_update_post, for example). To add an individual image to your post, you can add an <img> tag (perhaps using the get_image_tag function). If you want to add a gallery, use the [gallery] shortcode.

| improve this answer | |
  • Hi, I'm not trying to set a featured image,I know how to do that. I want to set a number of images as attachments so there is an association with the post. – Lee Sep 9 '13 at 22:06
  • @Lee I've expanded my answer. – Ben Miller - Remember Monica Sep 10 '13 at 5:07
  • 1
    these days (4.7+) post_mime_type needs to be set for the update to work properly. Can also just use wpdb to add the post_parent – chris May 5 '17 at 17:34
  • It should also be noted that these days wp_insert_attachment's third parameter is the parent field optionally expecting the ID of the post it will be attached to: wp_insert_attachment( string|array $args, string $file = false, int $parent, bool $wp_error = false ) So ideally that will be called during the upload/save process with wp_handle_upload moving the image to the correct location and returning the full meta thus allowing wp_insert_attachment attaching it to the post. – Brian Layman Dec 6 '18 at 20:58

I have decided to post it as a separate post and not a comment because it will be lost. I've tried Ben's solution but it modifies the attachment's other data fields. I ended up using this.

            $attachment = array(
                'ID' => $att_id,
                'post_parent' => $custom_post_id,

            $res = wp_update_post($attachment);
| improve this answer | |
  • 1
    Add clean_attachment_cache($att_id) to clear the media library cache. Took me a while to figure out why it wasn't working. This was it! – wired May 1 '18 at 22:45

This might be something you want to do through the GUI rather than programmatically. The following plugin will do what Ben Miller suggested in setting the post_parent to the custom post ID.

The plugin Unattach and Reattach Media Attachments works really well and has a bulk action built into it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.