Home › Forums › Front-end Issues › featured image from relationship field (backend)
Hi there,
I’ve been looking for this for a while now, but I can’t get a clue..
At the backend, I would like to grab the featured image from a relationship field and inset it as the current featured image at the current cpt.
Let me explain, I have two cpt, one it’s called “film-negative” and the other one it’s called “print-size”, I have set a featured image in “film-negative” cpt and on “print-size” cpt I have a relationship field where I’m calling “film-negative” ctp so then I can print out on my “print-size” template which film-negative I’m using for that print size. So, on the backend, I would like to grab the featured image of relationship “film-negative” and insert it on “print-size” cpt.
Sorry about my poor english.
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);
}
}
}
Hi @hube2 , thank you very much for your time.
If I understand correctly, I should paste this code on my function.php, and then where says ‘relationship_field_name’ I should write the relationship field from where I’m interested to get the thumbnail image? Because if it’s the case, it’s not working for me.
Then the image should appear in “featured image” on my “print-size” cpt without doing a thing, isn’t it?
uhmm, I’m saving the post but nothing happening.
just to make it clear for me, I should change the code just on: ‘relationship_field_name’ because the cpt name is already there: ‘print-size’ ?
There are some things that could make this not work.
1) What is the return value of the relationship field? Does it allow 1 or multiple selections?
2) Are you using the standard WP Featured image? Or are you using an ACF field for this? The code is based on the first options.
Hi @hube2 ,
1) the return value for the relationship field is set to “Post Objet” and I’m allowed to select more than one, but I’m always selecting just one “film-negative”.
2) I’m using the standard WP Featured image.
thank you.
If you can select multiple then ACF is returning an array. My code actually eliminates the return value so that doesn’t matter (forgot that), but we do have to deal with the possibility of an array return value. This will only work to get the first related post selected
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 (is_array($related_id) && !empty($related_id)) {
$related_id = $related_id[0];
}
if (!empty($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);
}
}
}
You must be logged in to reply to this topic.
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.