Basically, we are using the rest api to access custom post types created with toolset. We have 2 custom post types with parent-child relationships. For our filtering, we add the name of the assigned parent to each child element. This works great as long as we use the normal language.
Now if we use a page translated with WPML we do not get the associated parent page, rather we get the english title of the child page.
For example:
Parent Page - parent1
Child Page - child1
parentpage shown in the api for child1: parent1
But if child1 is in english we get the following for parentpage of child1: child1
Can anyone assist us with our issue? We added a screenshot.
Hello,
I suggest you try these:
1) Debug your custom PHP codes manually, for example this line:
$toolset_parent_id = toolset_get_related_post(...
Output the result, make sure $toolset_parent_id outputs correct parent post ID
2) If you want to get the parent post ID in specific language version, please follow WPML document to use filter "wpml_object_id", get the correct parent post ID:
https://wpml.org/wpml-hook/wpml_object_id/
Thank you for your reply Luo!
We now managed to retrieve the Post ID of the original, untranslated post, however it does not work, and 'toolset_get_related_post()' shows zero, meaning that no relationship was found. But the ID is definetly correct, and shows the right parent ID when receiving the german versin through the api. How can that be?
Please check these:
1) You are following our document to translate the parent/child relationships:
https://toolset.com/course-lesson/translating-related-content/
2) since it is a custom codes problem, if you still need assistance for it, please provide a test site with the same problem, fill below private message box with login credentials, also point out:
- Where I can edit your custom PHP codes
- Where and how I can test it in frontend
I need a live website to test and debug, thanks
We won't be able to provide you a test site until next week.
But the problem is very odd.
We tested to enter a hardcoded ID, but still, the function 'toolset_get_related_post' does not seem to work even when providing a full ID like so:
$toolset_parent_id = toolset_get_related_post(1378, 'comp-tour-comp-tourseite');
But if we switch to to the original language it works all of a sudden. How can that be?
The ID is hardcoded? It seems as if the function is not available for translated content, is that possible??
I have tried it in a fresh WP installation, it works fine, see below test site:
Login URL:
hidden link
1) Parent comp-tour posts:
hidden link
- English version, post ID is 37
- French version, post ID is 40
2) Create a shortcode with toolset_get_related_post function:
hidden link
add_shortcode('parent-post-info', function($atts, $content){
$tourseite_id = get_the_ID();
$parent_tour_id = toolset_get_related_post($tourseite_id, 'comp-tour-comp-tourseite');
return $parent_tour_id;
});
Display above custom shortcode in single child "comp-tourseite" post, and test it in frontend:
hidden link
Parent post ID: 37
hidden link
Parent post ID: 40
Can you reproduce the same problem in above test site?
Yes we still have the same issue, because we do not split the tours into two languages.
Meaning: We do not want to have Tour A EN and Tour A FR but rather have Tour A and then toursites EN and toursites FR.
In the end, we are retrieving the right parent id with the wpml functions but still it does not show up and toolset_get_related_post( results in 0 for our translated sites.
If there isn't parent post in secondary language, you will need to use get the child post ID in main language, then use this child post ID to get the related parent post ID in main language.
More help:
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#hook-605256
But I suggest you follow our document to translate the parent post too:
https://toolset.com/course-lesson/translating-related-content/
As mentioned in the previous answer. we already did that. We retrieved the parent post ID for the main-language with the same filter you sent us ( apply_filters('wpml_object_id' ...) ) and it works fine in the api for toursites in the main-language.
But it does not work for our translated sites. That's why it seems to be an issue with the toolset function 'toolset_get_related_post'.
The function only works when we call it via api for the main language. When we try the same function in the translated post, it does not work, although both times we call the function with the same id!
We can send you our code, maybe it is more clear then.
Please reproduce the same problem in above test site:
hidden link
I need a live website to test and debug this issue, thanks
We have reproduced the problem in your test site and uploaded our custom plugin which adds the metadata to api calls.
You can try the following:
1. Call the API with following hidden link
-> parentpagename shows the right parent page
2. Call the API for french posts like this hidden link
-> parentpagename shows the wrong value and toolset_get_related_post does not seem to work.
Can you confirm the problem on your end?
In both cases, parentpagename should show comp-tour A.
Thanks for the details, I am checking it in the test website, will update here if find anything
I have done below modifications in the test site:
1) Edit your custom plugin file:
hidden link
line 58~62, add below codes:
do_action( 'wpml_switch_language', 'en' );
$wpml_id_original_lang = apply_filters('wpml_object_id', $post_id, 'comp-tourseite', true);
$toolset_parent_id = toolset_get_related_post($wpml_id_original_lang, 'comp-tour-comp-tourseite');
var_dump($toolset_parent_id);
echo get_the_title($toolset_parent_id);
Test it in frontend:
hidden link
I can see correct parent post ID and title in English.
So you just need to switch to original language.
More help:
https://wpml.org/wpml-hook/wpml_switch_language/
For your reference.
Perfect, your changes worked fine!
We will test it more extensively, but it looks good so far.
Thank you very much for your effort!