Home › Forums › General Issues › Retrieving image url through relationship
I have an image field that returns the image url. It works as expected, except when I retrieve it through a relationship field. I use get_field to get the post object, select the first item in the array, and then select the image. Instead of getting the image url, I get the image id. I’m able to convert it to the url using wp_get_attachment_url, but curious why the get_field function returns the image id in the first place.
I would have to see the code, code that’s working and not. By your explanation I can’t see anything that should cause this.
Here’s the code:
function bank_acct_box($atts) {
$id = $atts['post_id'];
$childtag = $atts['child'];
$name = get_field('bank_account_name', $id);
$link = get_field('bank_account_affiliate_link', $id);
$bullets = get_field('bank_account_key_features', $id);
$bank = get_field('bank_institution', $id);
$image = $bank[0]->bank_logo;
$image = wp_get_attachment_url($image);
$btn = "<a class='btn btn-primary radius mt-3' href='$link/$childtag-btn' target='_blank' rel='nofollow'><strong>Open an Account</strong></a>";
$bankbox = "<div class='row bg-light p-3 mb-3 border'>
<div class='col text-center'>
<a class='btn btn-link mb-2' href='$link/$childtag-tl' target='_blank' rel='nofollow'><strong>$name</strong></a>
<img border='0' alt='$name' src='$image'>
</br>$btn
</div>
<div class='col-8'>$bullets</div>
$image<br></div>";
return "$bankbox";
}
add_shortcode( 'bankacct_box', 'bank_acct_box' );
Without this line ($image = wp_get_attachment_url($image);) it returns the image id, even though the field is set to return the image url AND it works when I’m not requesting it through the relationship field.
what type of field is bank_institution
$bank = get_field('bank_institution', $id);
$image = $bank[0]->bank_logo;
bank_institution is a relationship field and bank_logo is an image field.
I don’t know what
$image = $bank[0]->bank_logo;
is getting, but you can’t get an image field from a related post this way.
Code here depends on what the relationship is returning. I am assuming that you have it set to return a post object
$bank = get_field('bank_institution', $id);
$image = get_field('bank_logo', $bank[0]->ID);
You need to use the ID of the related post to get the field from that post
Well, my code works, I just need to convert the image id to the image url. An extra step that does’t make sense given that the image field I’m retrieving is set to return the url.
I personally do not see how this is even getting the correct image ID
$image = $bank[0]->bank_logo;
ACF does not get the post attachments and add them to the post array when getting the value of a relationship field. If this is being populated it is not being done by ACF. If it does contain the correct attachment ID then you need to look into how that value is being populated. That is where you’ll find the cause of it being an ID rather than a url.
It’s not a post attachment. The image url is saved in an ACF field. The above code is pretty standard.
Sorry, it’s not standard for me
$post_object->custom_field_name
will not get me the ACF field used on that post
I learn something new every day. I wanted to be sure I was not going crazy. This is something that I did not know until today. The WP_Post class has a magic __get() method that will return unknown properties.
However, in this case it is only returning the value for the image field that is stored in the database as a string. This bypasses the ACF by using get_post_meta() and this is why you are getting a string ID value instead of the value you expect.
Thank you for the enlightening topic. I like learning new things even after doing this for so many years.
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.