Skip Navigation

[Resolved] Get name of parent page for translations

This support ticket is created 2 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 13 replies, has 2 voices.

Last updated by Florian 2 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#2506013
2022-11-29 10_37_30-change-rest-api.php - Visual Studio Code.png

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.

#2506457

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/

#2507373
2022-12-01 13_45_33-https___compensationportal.kienbaum.com_en_wp-json_wp_v2_ComP-Tourseite_ - My Wo.png
2022-12-01 13_43_02-https___compensationportal.kienbaum.com_en_wp-json_wp_v2_ComP-Tourseite_ - My Wo.png
2022-12-01 13_42_24-● change-rest-api.php - Visual Studio Code.png

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?

#2507829

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

#2510257
2022-12-06 10_51_09-Window.png

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??

#2510833

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?

#2511597

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.

#2512145

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/

#2514433

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.

#2515835

Please reproduce the same problem in above test site:
hidden link
I need a live website to test and debug this issue, thanks

#2516089

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.

#2516659

Thanks for the details, I am checking it in the test website, will update here if find anything

#2519225

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.

#2519255

Perfect, your changes worked fine!
We will test it more extensively, but it looks good so far.

Thank you very much for your effort!