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
    Commented Sep 9, 2013 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
    Commented Sep 9, 2013 at 22:13
  • How are you planning to display the attachments to the user?
    – Cyrcle
    Commented Sep 10, 2013 at 3:57

2 Answers 2


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.

  • 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
    Commented Sep 9, 2013 at 22:06
  • @Lee I've expanded my answer.
    – Ben Miller
    Commented Sep 10, 2013 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
    Commented May 5, 2017 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. Commented Dec 6, 2018 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);
  • 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
    Commented May 1, 2018 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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