Home › Forums › Front-end Issues › Library: Uploaded to Post › Reply To: Library: Uploaded to Post
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');
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.