Home Forums Add-ons Gallery Field Gallery Fields not attaching images to custom post type Reply To: Gallery Fields not attaching images to custom post type

  • I could be wrong, but this is my understanding of things.

    When you’re in the admin on a post edit page and you click on “insert image” in the editor, or add images using an ACF field, these images are “attached” the current post that is being editor. WP has a post ID to associate the images with at the time they are inserted.

    If you select an image that is already attached to another post, this image does not get “attached” to the post you are editing. What post an image is “attached” to is set by the post_parent row in the database and a post (attachment) cannot have 2 parents.

    If you go to the media library and upload images, unattached images will be attached to the first post they are used on.

    When creating a new post in the admin, WP creates an “auto draft” of the new post and uses the id of this yet to be created post for attaching images.

    On a front end form if you are editing and existing post, in other words, the post ID used for acf_form() is the same as the post ID where the form is displayed, WP will also attach any image that are uploaded or added to the fields that are not already attached.

    In the case of adding a new post using an acf_form() there is no auto draft post created. When the images are uploaded WP does not know where to attach them to. It could be that they get attached to the post ID of the current post being shown or to nothing at all. I haven’t actually investigated this. There could be other cases, for example if your ACF form is shown on some type of archive page where there is not “post id”. It could be that the images are not attached anywhere, or possibly they are attached to the last post in the last loop. I also don’t know what happens when you’re editing a different post in ACF than the post where the form appears. All of these things will act in one way or another to confuse WP as to exactly where it should be attaching new images or adding images images that were not previously attached. The only thing that I am sure of is that if the image is already “attached” to another post.

    How can this be corrected?

    Add an update value filter to image and gallery fields. Check to the images to see if they are attached to the current post ID being saved. In order to keep with the way WP does things you should also make sure that they are not attached to another post. If they are not attached to something else then attach them to the current post by setting their post_parent value. But this will not help in the case where WP confuses where to attach them in the first place since it’s already attached them. In this case you would need to break from the way WP usually does things and always attach the image to the current post being edited.

    I don’t honestly see an acceptable way to correct this since WP does not keep any type of list anywhere telling you every location where an image is used and it does not have the ability to attach and image to more than one post….. unless I’m missing something.