Home › Forums › General Issues › Relationship field on translated pages
Hi there,
Until recently I had no problems with the ACF relationship fields and WPML. The only problem was that I could only get the English default relationship pages to appear on my duplicated language pages, but that was ok. Perhaps some update has changed it, but for some reason the ACF relations fields are no longer being displayed on my duplicated language pages (they are WPML duplicates, not independently translated pages). On the English pages they work fine, but with the same piece of code, nothing appears on the duplicated pages, although I can see in the duplicated post’s edit page that the fields have been copied correctly, and that I can see the right page is selected in the relationship selector.
This is the piece of code I use to get the relationship fields. On the English Default language they work, on the duplicate translations I get nothing (although in the back-end they do appear).
<?php
$previous_relationships = get_field('previous_relationships');
if($previous_relationships)
{
echo '';
foreach($previous_relationships as $previous_relationship)
{
echo '<a li href=' . get_permalink( $previous_relationship->ID ) . '>' . $previous_relationship->first_name . '</a></li><br>';
}
echo '';
}
?>
Does anyone know what is going on?
Hi @markkro
Did you add the code outside of The Loop? If you did, you need to pass the post/page ID as the second parameter to the get_field() function like this:
get_field(‘previous_relationships’, 99)
Where “99” is the translated post/page ID where the custom field is assigned. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/get_field/.
Could you also try to debug it like this:
ehco "<pre>";
var_dump( get_field('previous_relationships', false, false) );
ehco "</pre>";
Also, could you please share the template file of the page so I can check it out?
If you could share the JSON or XML export file of the field group for both of the languages, that would be great too!
Thanks 🙂
Hi @markkro
I’m afraid the files is not uploaded properly. For the PHP file, could you please copy and paste it here: https://gist.github.com/ or use a third-party online storage solution?
Thanks 🙂
I found a solution myself. I don’t know why it does the trick, but for other people facing the same problem, you need to set the ACF relationship field of the translated field group to return post ID’s instead of post objects, and then you can call them like:
<?php
$posts = get_field('relationship_field');
if( $posts ): ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
And change relationship_field to the name of your relationship field.
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.