Skip Navigation

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

Problem:
The client has a custom post type with translated slugs, but occasionally the archive page URL for the English site shows the Dutch slug instead of the English one. This issue is temporarily resolved by resetting the permalinks, but it can recur.

Solution:
The problem occurs because, when registering the custom post types, the client uses a gettext call to define the post type slug. We recommend removing the gettext call from the rewrite slug in the custom post type registration and using a static text instead. Here's how to adjust the code:

 'rewrite' => array('slug' => 'vegetarian-products', 'with_front' => false), 

After making this change, go to WPML > Settings > Post Types Translation to set the slug translations for your custom post type. If you prefer, you can also translate the slug using the String Translation feature, searching for the slug under the WordPress text domain.

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.

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 1 year, 6 months ago.

Assisted by: Nigel.

Author Posts
October 10, 2023 at 9:15 am #14547809

robbertd

I have a site with English as an origin language and Dutch as a translation.
On that site I have custom post types with translated slugs.

The CPT's are added as follows

```
function clientname_product_post() {
// creating (registering) the custom type
register_post_type(
'clientname_product',
array(
'labels' => array(
'name' => __('Products', 'redacted'),
'singular_name' => __('Product', 'redacted'),
'all_items' => __('All Products', 'redacted'),
'add_new' => __('Add', 'redacted'),
'add_new_item' => __('Add new Product', 'redacted'),
'edit' => __('Edit', 'redacted'),
'edit_item' => __('Edit Product', 'redacted'),
'new_item' => __('New Product', 'redacted'),
'view_item' => __('View Product', 'redacted'),
'search_items' => __('Search Products', 'redacted'),
'not_found' => __('Nothing found in the Database.', 'redacted'),
'not_found_in_trash' => __('Nothing found in Trash', 'redacted'),
'parent_item_colon' => ''
),
'description' => __('This is a Product', 'redacted'),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'query_var' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-carrot',
'rewrite' => array('slug' => _x('vegetarian-products', 'URL slug', 'redacted'), 'with_front' => false),
'has_archive' => 'product-archive',
'capability_type' => 'post',
'hierarchical' => true,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'revisions', 'sticky')
)
);

}
add_action('init', 'clientname_product_post');
```

The translation of *vegetarian-products* is added via WPML String translation and is *vegetarische-producten*

This all works well, but sometimes on the products archive page on the English site the url of a single products is hidden link;. Which is incorrect.

This also happens on another CPT

It is usually fixes by resetting the permalinks. Sometimes it needs multiple resets.

How is this possible?

Unfortunately I can't add a link because this happens sometimes and resetting the permalinks helps.

Screenshot 2023-10-10 at 11.08.44.png
October 10, 2023 at 1:39 pm #14550617

Nigel
WPML Supporter since 02/2016

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

Hi there

I think the problem arises because of your use of a gettext call when specifying the rewrite slug, here:

'rewrite' => array('slug' => _x('vegetarian-products', 'URL slug', 'redacted'), 'with_front' => false),

I would expect you to use a static text in this situation, and then translate the slug with WPML.

So change that to

'rewrite' => array('slug' => 'vegetarian-products', 'with_front' => false),

Then go to WPML > Settings > Post Types Translation and locate your custom post type, and you should be able to provide slug translations there (as in my screenshot from my test site, with a post type "Things").

Alternatively, you can translate the slug via String Translation (you should be able to find the slug under the WordPress text domain).

Screenshot 2023-10-10 at 12.19.08.png