Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
You are experiencing issues with translated posts not being found due to complex permalink structures and custom rewrite rules in a multilingual WordPress setup using WPML.
Solution:
We recommend modifying the timing of your redirect rules. Instead of loading your redirects on 'init', try using the 'wpml-loaded' action. This ensures that WPML's language handling is fully loaded before your custom rewrite rules are applied, which helps in maintaining the correct language directories in URLs. For more details on this hook, please visit https://wpml.org/wpml-hook/wpml_loaded/.

If this solution does not resolve your issue or seems outdated, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. Should you need further assistance, please do not hesitate to open a new support ticket at WPML support forum.

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 18 replies, has 2 voices.

Last updated by Andreas W. 2 months ago.

Assisted by: Andreas W..

Author Posts
September 23, 2024 at 6:02 pm #16211348

davidH-179

<b>Background of the issue: </b>
I am working on a site under development and trying to use English slugs with WPML. It is working on pages and posts. But not on custom post types

<b>Symptoms: </b>
The language switcher is not working after changing to using English slugs.

<b>Questions: </b>
Why is the language switcher not working after changing to English slugs?

September 23, 2024 at 6:32 pm #16211443

Andreas W.
Supporter

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

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

Hello,

Is your CPT set to be translatable inside WPML > Settings > Post Type Translation?

Guide:
https://wpml.org/documentation/getting-started-guide/translating-custom-posts/

Usually, you further should translate the CPT slug with WPML > String Translation.

Guide:
https://wpml.org/documentation/support/translating-slugs-for-custom-post-type-archives/

If this did not clarify the cause of the issue, was this CPT integrated into your theme or was it created with ACF or another plugin?

Best regards
Andreas

September 23, 2024 at 7:37 pm #16211679

davidH-179

Yes we've set them to be translateable.

The String translation is not the issue I don't think. I think the issue may be because we have a complicated permalinks structure:

<?php

namespace App\Providers;

use Roots\Acorn\Sage\SageServiceProvider;

class RewriteRulesServiceProvider extends SageServiceProvider {
public function register() {
//
}

public function boot() {
add_action( 'init', [ $this, 'addRewriteRules' ] );
add_filter( 'post_type_link', [ $this, 'customPostTypeLink' ], 10, 2 );

// Add rewrite rule for /search
add_action( 'init', function () {
add_rewrite_rule( '^search/?$', 'index.php?s=', 'top' );
} );

// Add 'search' to the query vars
add_filter( 'query_vars', function ( $vars ) {
$vars[] = 'search';

return $vars;
} );

// Redirect /search to search template
add_action( 'template_redirect', function () {
if ( get_query_var( 'search' ) !== '' ) {
include( get_template_directory() . '/search.php' );
exit();
}
} );
}

public function addRewriteRules() {
add_rewrite_rule(
'^global-immigration/market/([^/]+)/?$',
'index.php?post_type=market&name=$matches[1]',
'top'
);

add_rewrite_rule(
'^global-immigration/([^/]+)/?$',
'index.php?post_type=global_immigration&name=$matches[1]',
'top'
);

add_rewrite_rule(
'^global-immigration/market/([^/]+)/([^/]+)/?$',
'index.php?post_type=office&name=$matches[2]',
'top'
);

add_rewrite_rule(
'^global-immigration/market/([^/]+)/([^/]+)/([^/]+)/?$',
'index.php?post_type=team&name=$matches[3]',
'top'
);

add_rewrite_rule(
'^uk-immigration/([^/]+)/?$',
'index.php?post_type=uk_immigration&name=$matches[1]',
'top'
);

add_rewrite_rule(
'^uk-immigration/([^/]+)/([^/]+)/?$',
'index.php?post_type=visa&name=$matches[2]',
'top'
);

add_rewrite_rule(
'^team/([^/]+)/?$',
'index.php?post_type=team&name=$matches[1]',
'top'
);

add_rewrite_rule(
'^visa/([^/]+)/?$',
'index.php?post_type=visa&name=$matches[1]',
'top'
);

add_rewrite_rule(
'^news/([^/]+)/?$',
'index.php?post_type=news&name=$matches[1]',
'top'
);
}

public function customPostTypeLink( $post_link, $post ) {
if ( $post->post_type == 'market' ) {
return home_url( 'global-immigration/market/' . $post->post_name );
}

if ( $post->post_type == 'office' ) {
$market = get_field( 'market', $post->ID );
if ( $market ) {
$market_post = get_post( $market );

return home_url( 'global-immigration/market/' . $market_post->post_name . '/' . $post->post_name );
} else {
return home_url( 'global-immigration/market/' . $post->post_name );
}
}

if ( $post->post_type == 'team' ) {
$office = get_field( 'office', $post->ID );
if ( $office ) {
$office_post = get_post( $office[0] );
$market = get_field( 'market', $office_post->ID );
if ( $market ) {
$market_post = get_post( $market );

return home_url( 'global-immigration/market/' . $market_post->post_name . '/' . $office_post->post_name . '/' . $post->post_name );
}
} else {
return home_url( 'team/' . $post->post_name );
}
}

if ( $post->post_type == 'visa' ) {
$related_post = get_field( 'area_of_expertise', $post->ID ) ? get_field( 'area_of_expertise', $post->ID )[0] : null;

if ( $related_post ) {
if ( $related_post->post_type == 'uk_immigration' ) {
return home_url( 'uk-immigration/' . $related_post->post_name . '/' . $post->post_name );
} elseif ( $related_post->post_type == 'global_immigration' ) {
return home_url( 'global-immigration/' . $related_post->post_name . '/' . $post->post_name );
}
} else {
return home_url( 'visa/' . $post->post_name );
}
}

return $post_link;
}
}

September 24, 2024 at 3:17 am #16212253

Andreas W.
Supporter

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

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

Hello,

If we are talking about the product archive slug, then translating it with String Translation would be the correct way.

Did you try this already?

I would like to take a closer look into this issue.

I can offer to have a closer look at the site as an admin or provide a test site with WPML on which you can recreate the issue by installing your theme and the necessary plugins so that I can assist you on this matter.

Which way would you prefer?

Best regards
Andreas

September 24, 2024 at 8:33 am #16212963

davidH-179

yes you can be admin, how should I send the details?

September 24, 2024 at 11:33 am #16214280

Andreas W.
Supporter

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

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

Hello,

I would like to offer to have a closer look at the problem and request temporary access (wp-admin and FTP) to the website to investigate this issue further.

You can find the required fields below the comment section when you log in to leave the next reply. The information you provide is private, which means only you and I can see and access it.

IMPORTANT
Please be sure to make a backup copy of the website and database before allowing us access.
If you can't see the wp-admin / FTP fields, your post and website credentials are set as PUBLIC. DO NOT publish the data unless you see the required wp-admin / FTP fields.

I may have to install a plugin called "All In One WP Migration" to make a copy of the website where I can investigate the issue further.

However, I would also be very grateful if you could provide a staging site or copy of the website from your server yourself for this purpose. This step would only be required if the issue is replicable on such a staging site.

If you have any questions about creating such a staging site, you can consult your hosting provider. Just take note that WPML should be registered on this site again.

If you are unable to provide such a copy of the site for testing, please let me know on this ticket.

The private response form looks like this:
hidden link

Next time you reply, click "I still need assistance."

Video:
hidden link

Please note that we are obliged to request this information individually on each ticket. We may not access any access information not specifically submitted on this ticket in the private response form.

Best regards
Andreas

September 24, 2024 at 1:20 pm #16215166

davidH-179

And this is a general site password you will need : cqc7kdy9ayz*mcu-TBA

September 24, 2024 at 2:37 pm #16215635

davidH-179

?

September 24, 2024 at 2:37 pm #16215636

davidH-179

? did you receive

September 24, 2024 at 2:42 pm #16215642

Andreas W.
Supporter

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

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

Yes, I am looking into this in a moment.

September 24, 2024 at 3:02 pm #16215815

davidH-179

Ok thank you

September 24, 2024 at 9:40 pm #16217655

Andreas W.
Supporter

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

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

I can see translated CPTs here:
hidden link

And here:
hidden link

I enable the slug translation inside the WPML settings.

You can translate slugs for these post types not at WPML > Setttings > Post Type Translation.

See screenshots.

slug translation.jpg
slug translation 2.jpg
September 27, 2024 at 9:50 am #16229270

davidH-179

Thanks, thats helped I think, most of the pages are translating well.

But the below type of pages are not working. When I use the language switcher it does not change to chinese...

Office pages:
hidden link

Market Pages
hidden link

Bio pages
hidden link

September 27, 2024 at 9:59 am #16229308

davidH-179

And visa pages:
hidden link

September 27, 2024 at 1:15 pm #16230016

davidH-179

Is it possible to get assistance on this, we are launching this site on Monday.

In addition to the above there are a couple of other small things that I'd like you to look at like the breadcrumbs are not translating either