Hi Philippe,
I forgot to tell you that this only works if the field is returning an array because it needs an id.
Your field should look like :
Try to change this on your acf field.
After this, you might need to change what’s on your page templates if you were using url or id before.
Hi Philippe,
You might need to rename the actual function.
Try to change it to something like my_acf_save_post()
Don’t forget to update the add_action line as well.
To anyone having this problem, We did this and it works fine :
function acf_save_post( $post_id ) {
$file = get_field('upload_a_file', $post_id);
if( $file ){
$file_id = $file['ID'];
$post_to_update = array(
'ID' => $file_id,
'post_parent' => $post_id
);
wp_update_post( $post_to_update );
}
}
add_action('acf/save_post', 'acf_save_post', 20);
Replace upload_a_file with your correct acf field.
Basically we just modify the actual file post’s ‘post_parent’ to where it’s uploaded to. The attached file from acf will now show attached to the correct parent post.
We solved this problem by this :
function acf_save_post( $post_id ) {
$file = get_field('upload_a_file', $post_id);
if( $file ){
$file_id = $file['ID'];
$post_to_update = array(
'ID' => $file_id,
'post_parent' => $post_id
);
wp_update_post( $post_to_update );
}
}
add_action('acf/save_post', 'acf_save_post', 20);
Replace upload_a_file with your correct acf field.
Basically we just modify the actual file post’s post_parent to the one it’s uploaded to.