Hi all,
I’m using a page with acf_form() that pulls through the fields for an ID that’s just passed as a query string. I’ve got a gallery and was hoping to just allow uploads to that query string ID but it’s uploading the media files to the ID of the page.
My question is, is there a way to change the ID of where the images will be saved much like I can do with the acf_form post_id?
After looking into this, the best I can come up with is to add an acf/save_post action.
In this action you get a list of the images for the gallery
// this will get unformatted value of gallery
// an array of attachment post IDs
// see https://www.advancedcustomfields.com/resources/get_field/
$gallery = get_field('gallery_field_name', $post_id, false);
Then loop over this list of ID, get the attachment post using get_post(), change the “post_parent” value and then use wp_update_post() to save the changes.
Notes: An attachment can only go “attached” to a single post
You must remove your action, inside of the action, before calling wp_update_post(). If you don’t then you create an infinite loop because acf/save_post is triggered by calling that function.
add_action('acf/save_post', 'your_function_name');
function your_function_name($post_id) {
remove_filter(('acf/save_post', 'your_function_name');
}