I'm trying to upload some associated media to a custom post type.

Using file_put_contents() I'm uploading an image and trying to set an ACF image field to be the path to that file.

But reading the documentation I apparently need to supply an attachment_id as part of the update_field parameters. However, if I insert the image with wp_insert_attachment (to get an attachment id) it's going to be set as the featured image, will it not?

How can I get an attachment_id without overriding the pre-existing featured image?

  • No, attaching an attachment to a post does not set it as the featured image, that's not how featured images work ( and is very easy to test ). E.g. if you add a gallery of 5 images that's 5 attachments, but you don't get 5 featured images. You should also avoid file_put_contents and you should look up the ACF image field documentation, this stack isn't a place to get help with ACF, 3rd party plugin dev support is offtopic here
    – Tom J Nowell
    Jul 13, 2022 at 12:25
  • Why should file_put_contents be avoided
    – Fluxian
    Jul 13, 2022 at 13:32
  • because the media library is not a file explorer, putting things in wp-content/uploads does not make them appear in WordPress, there are already functions provided for sideloading files in, either from remote URLs, or uploads, that will take the file and put it where it needs to be while respecting the settings in WP Admin, creating the attachment post, filling in its metadata and firing al the necessary hooks
    – Tom J Nowell
    Jul 13, 2022 at 15:22
  • Such as what functions
    – Fluxian
    Jul 13, 2022 at 15:46
  • Most if not all of these: developer.wordpress.org/?s=sideload, note though that comments are for asking clarifying questions about the original question itself rather than having discussions, and we're moving away from the question here. If you've a new question feel free to create a new Q to keep this on subject
    – Tom J Nowell
    Jul 13, 2022 at 16:09

2 Answers 2


You should read up on the wp_insert_attachment function. You can check it out here https://developer.wordpress.org/reference/functions/wp_insert_attachment/. It does not automatically set the image as a featured image. For that you would need set_post_thumbnail.


However, if I insert the image with wp_insert_attachment (to get an attachment id) it's going to be set as the featured image, will it not?

No, it will not, that's not how featured images work. You have to explicitly and deliberately set the featured image for it to change.

For example, if this was how it worked, your featured image would change every time you added an image to your posts content.

All images and documents you upload to WordPress and the media library are represented as attachments, aka a post of type attachment. The parent of that attachment is what it's attached to, and a post can have many many attachments. E.g. all the images you upload when editing a post become attached. This is also what the media library displays, dropping a PDF or JPEG in the uploads folder won't make it show in the media library.

So no, creating an attachment won't immediately change the featured image.

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.