Skip Navigation

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.

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

Supporter timezone: America/Lima (GMT-05:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by lorenzP 2 hours, 15 minutes ago.

Assisted by: Andreas W..

Author Posts
January 28, 2025 at 5:48 pm #16643543

lorenzP

Background of the issue:
I am trying to use url_to_postid() to convert a URL to a post ID. For instance, the following call always returns 0: url_to_postid('hidden link'). This happened recently, before there was no such issue. I tried to delete various WPML caches but this made no difference. Link to a page where the issue can be seen: hidden link

Symptoms:
The API should return the post ID, but instead, I got 0.

Questions:
Why does url_to_postid() return 0 for a valid URL in the secondary language?
How can I resolve the issue of url_to_postid() not returning the correct post ID?

January 29, 2025 at 8:23 am #16644795

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+01:00)

Hi,

before your ticket is assigned to one of my colleagues, please allow me to walk you through some initial debugging steps. This will help speed up the support process.

I came across an old, archived ticket from you from 2023, where you reported a similar issue with this function while using PHP 8.0:

We found the root cause: the URL parameter was in some occasion a SimpleXMLElement instance and not a string. We added a type cast to a string and it works now fine.

Have you checked if this change is still in place?

Best Regards
Marcel

January 31, 2025 at 5:31 pm #16656847

lorenzP

This is a different issue, the parameter is a string. For instance this call is failing and returns 0 for an existing URL:

url_to_postid('hidden link')

January 31, 2025 at 8:33 pm #16657188

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

Each post in each language has its own ID.

You might need to use the following hook in this case:
https://wpml.org/wpml-hook/wpml_object_id/

To get the translated URL please use this hook:
https://wpml.org/wpml-hook/wpml_permalink/

Best regards
Andreas

February 3, 2025 at 2:43 pm #16663422

lorenzP

I think you don't understand our problem. The url_to_postid() API is failing. We have to convert a URL to a post ID and not vice versa. This was working fine in the past and suddenly stopped working.