Home›Support›English Support›[Resolved] Post Relationship Field (from Meta Box) not automatically linked in ATE
[Resolved] Post Relationship Field (from Meta Box) not automatically linked in ATE
This thread is resolved. Here is a description of the problem and solution.
Problem: You are trying to synchronize a 'Post' relationship field automatically when translating content using the Advanced Translation Editor (ATE). You have two Custom Post Types (CPTs): a parent CPT and a child CPT. The child post uses a 'Post' relationship field to select the parent, but when translating from English to German, the field in the German post does not automatically populate with the ID of the corresponding German parent, leaving the relationship field empty. Solution: This issue is recognized and will be addressed in a future WPML release (version 4.8). Meanwhile, you can use the
wpml_object_id
hook as a workaround. Set the custom field to 'COPY' so that the translation retains the POST ID (even if it's not the translated one). Use the following PHP code to translate the POST ID in the current language:
This code will help display the correct translated post ID on the frontend. Remember, the empty field in the backend is a visual bug and does not affect the actual functionality.
If this solution does not apply to your case, or if it seems outdated, we recommend opening a new support ticket. Also, check related known issues at https://wpml.org/known-issues/, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. For further assistance, please contact our support at WPML support forum.
This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
Background of the issue:
I am trying to synchronize a post relationship field automatically when translating content using the Advanced Translation Editor (ATE). I have two Custom Post Types (CPTs): a parent CPT with the slug 'travel-ticket' or 'activity', and a child CPT with the slug 'details'. On the child post, I use a required 'Post' relationship field from the Meta Box plugin (field ID: post_hierarchy_parent) to select the parent travel-ticket or activity. I have set post_hierarchy_parent to 'Translate' in WPML > Settings > Custom Fields Translation and attempted to use Custom XML Configuration: post_hierarchy_parent. However, this did not resolve the issue, so I removed it. If I manually edit the translated German child post and select the German parent from the dropdown, it saves correctly, indicating no theme or Meta Box conflicts. The issue seems to be with ATE's automatic linking process.
Symptoms:
When I translate a child post from English to German using ATE, the post_hierarchy_parent field on the German post is not automatically populated with the ID of the corresponding German travel-ticket. Instead, the field is empty, and the relationship is not created automatically.
Questions:
Why is the ATE failing to automatically link this standard relationship field?
I created a parent and child post. The child post points to the parent via the only custom field. The child post was translated, but the custom field of the German version remains empty.
So, the English child points to the English parent but the German child does not point to anything.
so it seems to be a know issue that will be fixed in WPML 4.8, in the mean time you can use wpml_object_id hook and it should work fine.
So here is an example of workaround and code used in sandbox test site:
- Keep the custom field parent_post as COPY, that way the translation will always have a POST ID (even if it's not he translated one).
We don't know how you are coding this field into your template, so we created an example on your sandbox to explain how to use wpml_object_id.
We edited the theme single.php template: hidden link
We added the following code right after get_template_part( 'template-parts/content/content-single' ); line 18.
// TEST WPML
// Getting selected post ID:
$post_id = rwmb_meta( 'parent_post' );
?>
<p>Selected post ID (original EN ID): <?php echo $post_id ?></p>
<?php
$current_lang = apply_filters( 'wpml_current_language', NULL );
$post_id = apply_filters( 'wpml_object_id', $post_id, 'parent-post', TRUE, $current_lang );
?>
<p>Translated post ID: <?php echo $post_id ?></p>
<p>
Only to show it will work with default WP functions such as get_permalink or get_the_title:
</p>
<h3><a href="<?php echo get_permalink( $post_id ) ?>"><?php echo get_the_title( $post_id ); ?></a></h3>
<?php
The first part is to print the post ID selected (which will be the EN post ID)
Then we use wpml_object_id to translate it in the current language
We print again the post ID to show the difference
Last we gave a real use case example
To see it in action go to hidden link
Hope this helps out, let us know if any further questions or doubts.
thanks, even if the field looks empty on 2nd language page, in DB the correct value is saved/ showed, it is only visible bug, and for frontend you should use suggested approach.
1) Yes it should always be set to copy, if translate then you need to do manual translation and adjust the field value manually.
2) We have it planned for few week, but I can not give any ETA.
as I have said above, that is expected it will show empty in backend, but actually in database it contains correct value, it is visual bug.
In such case and as plugin usage doc are explained you need to use PHP code to show field value on frontend, and for such we have provided workaround and how to use it and show correct translated post value.
What you see empty in backed, is just a visual a bug and will be fixed in new WPML version.