Languages: English (English )Spanish (Español )German (Deutsch )
Timezone: America/Lima (GMT-05:00)
We ran a quick test and could not confirm an issue in a minimal setup: hidden link
Your code works as expected here without any workaround.
---
Now, I saw you reported this issue earlier, and we have a couple of open tickets on development about this issue, which are still not solved and this is why I tried to provide a possible workaround.
My previous answer truly was not AI-generated and I have today verified the provided workaround with one of our compatibility developers, who suggested a slight change:
// Use the wpml_current_language hook to get the current language
$current_language = apply_filters( 'wpml_current_language', NULL );
// Get the permalink for a specific post in default language (replace with your post ID)
$default_language = apply_filters( 'wpml_default_language', NULL );
do_action( 'wpml_switch_language', $default_language );
$post_permalink = get_permalink( $post_id );
// Get the post ID from the URL (this works for the default language, based on permalink)
$post_id_in_default_language = url_to_post_id( $post_permalink );
// Use WPML's filter to get the translated post ID in the current language
do_action( 'wpml_switch_language', $current_language );
$translated_post_id = apply_filters( 'wpml_object_id', $post_id_in_default_language );
// $translated_post_id now contains the post ID for the current language
echo $translated_post_id;
---
If it works on the production site and not on the staging then this would mean both sites are not identical. Revise installed theme and plugins and their versions. Make sure that staging and production use the same custom code.
If you would like me to take a look at the staging, let me know.
---
Also, I would like to invite you to, please log into the following test site and recreate the issue, so that we can take a closer look at your code and try to find a solution:
The code you provided assumes we know the post ID already (calls get_permalink()) but we don’t know the post ID that’s why we are using the url_to_postid() WordPress API to convert a URL to a post ID.
Last week I debugged the WPML plugin’s source code for more than 4 hours to understand what’s going wrong. The WPML hook handles language switching, URL parameter stripping and lots of other things. I found the following code was failing and a failsafe code gets executed which returns the 0 result:
In classes/query-filtering/class-wpml-get-page-by-path.php the WPML_Get_Page_By_Path::get() method uses get_page_by_path() internally and returns no WP_Post value in our case. This method gets called with $post_name as parameter but does not get the full path of the page which is in our case a child page.
In our production environment we get the correct post ID but in our staging environment this failed to work about three weeks ago.
In order to be able to launch our new website ASAP, we added a workaround which uses get_page_by_path() in cases where url_to_postid() returns 0. This works fine for now without having a fix yet for the root cause.
We also extended the test page to show the APIs we call:
hidden link
To further analyze this issue we can get you access to our staging environment or duplicate it on one of your servers as we have done before. I guess this issue is related to the database and therefore not easy to reproduce on a fresh WordPress instance.
Languages: English (English )Spanish (Español )German (Deutsch )
Timezone: America/Lima (GMT-05:00)
To report a bug in WPML and for further escalation I would like to invite you to, please log into the following test site and recreate the issue, so that we can take a closer look at your code and take further steps: