Skip to content Skip to sidebar

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.

Tagged: 

This topic contains 6 replies, has 3 voices.

Last updated by Waqas Bin Hasan 1 year, 12 months ago.

Assisted by: Waqas Bin Hasan.

Author Posts
August 1, 2023 at 10:19 pm #14137995

Josh Warren-White

Tell us what you are trying to do?
We've got a custom post type on our site, media-section, which though it's set to be translatable leads to the user to a 404 on translations. This is only happening for this CPT, but appears to be happening for all of them.
hidden link

Is there any documentation that you are following?
I followed the troubleshooting steps in this post hoping they might also help us, but they did not.
https://wpml.org/forums/topic/custom-post-types-sometimes-opens-and-sometimes-gets-404-error-page-not-found/#post-13954609

Is there a similar example that we can see?
It also happened when we wrote a now deleted translation for this post
hidden link
and for this post
hidden link

What is the link to your site?
hidden link

August 2, 2023 at 8:38 am #14139957

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

I am checking the link you shared but I see it works for 2nd language:

- hidden link
- hidden link

Let us know if you still need our help and steps on how can we see the issue.

Thanks,
Drazen

August 2, 2023 at 7:23 pm #14144575

Josh Warren-White

Thanks for looking Drazen, but it's the Spanish translation that doesn't work. Spanish is in fact the only translation written for this post. Though the url "works", French is falling back to the English cause it has not been translated to French yet.

Here's the Spanish link, you can see that it goes to a 404.
hidden link

Or when you land on the English link and then hit the translate button it also going to a 404.

The other one is in English and Portugese
hidden link

These are the only 2 posts of this post type, media-section, that have translations written and neither work.

Thanks for your continued support!

August 3, 2023 at 1:12 pm #14149839

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Josh,

Thank you for contacting the support. I am assigned this ticket and can see the issue on the links you mentioned in your recent message.

Before proceeding further, may I ask you to try the following?

IMPORTANT: Take full backup of your website and database beforehand. It is highly advised to try the following on a staging/dev site first, so your live website isn't compromised.

- Deactivate all plugins except WPML and its addons and check for the issue.
- Activate each plugin one-by-one, while keeping check on the issue after a plugin is activated. This way you'll be able to pinpoint a plugin having a conflict (or problem), if any.
- If the problem persists, repeat the above steps with a default theme of WordPress (i.e. 2021 or 2023).

Please let me know and I'll try my best to help you further on the matter.

Regards.

August 3, 2023 at 7:04 pm #14151535

Josh Warren-White

This was very helpful Waqas! Thank you!

I narrowed the issue down to a pre_get_posts function we added to be able to order these custom posts by an ACF date custom field.

When removed the media-section posts load in their respective translations. When there the media-section posts go to a 404.

Do you have any tips on how I might modify this pre_get_posts function to make it compatible with WPML?

Thank you!

This is what the function looks like.

function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}

// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'media-section' || isset($query->query_vars['media-type'])) {

$query->set('orderby', 'meta_value');
$query->set('meta_key', 'publication_date_2');
$query->set('order', 'DESC');
$query->set('posts_per_page', 9);

}

// return
return $query;

}

add_action('pre_get_posts', 'my_pre_get_posts');

August 3, 2023 at 7:07 pm #14151537

Josh Warren-White

ps I tried setting that field to Do not translate, copy, copy once and translate. None of these setting had any effect.

August 4, 2023 at 7:36 am #14153191

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the updates Josh.

Unfortunately the custom coding is out of scope of this forum. But I suspect you may need to take the current language (or language of the posts) into account when querying the posts.

I'll recommend checking following guides:

- https://wpml.org/documentation/support/wpml-coding-api/
- https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
- https://wpml.org/documentation/support/wpml-coding-api/shortcodes/
- https://wpml.org/faq/how-to-get-current-language-with-wpml/
- https://wpml.org/documentation/support/wpml-tables/
- https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/
- https://wpml.org/documentation/support/