Home › Forums › Front-end Issues › Image not attached to post on save acf form › Reply To: Image not attached to post on save acf form
John Huebner’s reply and this post sent me into the right direction:
https://support.advancedcustomfields.com/forums/topic/acf_form-attachment-to-media-gallery/
Modified it for gallery field.
function my_save_post( $post_id ) {
//attachment to media gallery
$my_files = get_field('my_gallery', $post_id);
if($my_files){
foreach( $my_files as $my_file ):
$file_id = $my_file['ID'];
$post_to_update = array(
'ID' => $file_id,
'post_parent' => $post_id
);
wp_update_post( $post_to_update );
endforeach;
}
}
add_action('acf/save_post', 'my_save_post', 15);
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.