Home › Forums › Front-end Issues › featured image from relationship field (backend) › Reply To: featured image from relationship field (backend)
You need to do this with an acf/save_post action.
add_action('acf/save_post', 'copy_image_from_relationship', 20);
function copy_image_from_relationship($post_id) {
// check the post type
if (get_post_type($post_id) != 'print-size') {
// not the right post type
return;
}
// get the post id for the related post
$related_id = get_field('relationship_field_name', $post_id, false);
if ($related_id) {
// get the featured image ID from the related post
$image_id = get_post_thumbnail_id($related_id);
if ($image_id) {
// set on current post being updated
update_post_meta($post_id, '_thumbnail_id', $image_id);
}
}
}
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.