Skip Navigation

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

Problem:
The client needed to translate archive pages for custom post types (CPTs) and deactivate/delete unwanted indexed translated archive pages.
Solution:
We recommended manually registering the CPTs to avoid issues with translations and indexing. Additionally, the client provided a custom code snippet to disable the archive slug for some CPTs in all languages other than the default. Here is the code snippet:

function sws_remove_cpt_archive_for_other_languages() {<br />    $current_language = apply_filters('wpml_current_language', null);<br />    $default_language = apply_filters('wpml_default_language', null);<br />    if ($current_language !== $default_language) {<br />        add_action('template_redirect', 'sws_disable_cpt_archive');<br />    }<br />}<br />function sws_disable_cpt_archive() {<br />    $cpts_to_disable = array('cpt1', 'cpt2', 'cpt3');<br />    foreach ($cpts_to_disable as $cpt) {<br />        if (is_post_type_archive($cpt)) {<br />            wp_redirect(apply_filters('wpml_home_url', home_url()));<br />            exit();<br />        }<br />    }<br />}<br />add_action('init', 'sws_remove_cpt_archive_for_other_languages', -1);

We also suggested modifying the

\MBCPT\PostTypeRegister::get_post_types

method in

wp-content/plugins/meta-box-aio/vendor/meta-box/mb-custom-post-type/src/PostTypeRegister.php

by setting

suppress_filters = false

. This ensures

get_posts

will fetch posts only in the specific language by bypassing WPML filters.

If this solution does not apply to your case, or if it 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. If further assistance is needed, please open a new support ticket.

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

Last updated by Bruno Kos 6 months, 2 weeks ago.

Assisted by: Bruno Kos.

Author Posts
July 8, 2024 at 12:52 pm

adrienR-5

Background of the issue:
We have some links that are indexed of some archive page and while we don't know how to translate those archive pages in the first place, we would like to know how we can deactivate/delete those indexable URLs which aren't wanted.

Symptoms:
We don't know how to translate archive pages of custom CPT and want to remove unwanted indexed translated archive pages.

Questions:
How to translate archive pages of custom CPT?
How to deactivate/delete unwanted indexed translated archive pages?

July 8, 2024 at 1:33 pm
July 9, 2024 at 8:21 am #15918883

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

To troubleshoot this problem, I'll install the Duplicator plugin and generate packages for further debugging purposes. I'll ensure to exclude all media files to maintain a minimal package size. You can find more information about the process here: [link](https://wpml.org/faq/provide-supporters-copy-site/). Please confirm if this approach is acceptable to you.

July 9, 2024 at 11:26 am
July 9, 2024 at 2:41 pm #15921496

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

Escalated to 2nd Tier. I will keep you posted.

July 9, 2024 at 7:39 pm #15922954

adrienR-5

Hello Bruno,

I cannot update any translates pages or posts anymore, I don't understand why and this is critical.

Did you changed something inside WPML or somewhere else?

I have a red notice when I try to update: "Updating failed. The response is not a valid JSON response."

July 10, 2024 at 12:15 am #15923806

adrienR-5

The issue posted just in my comment above is resolved.

Please update me on the issue about archive page and translations.

July 10, 2024 at 5:20 am #15925422

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

I did not change anything, in fact I don't even have access to your site as logins don't seem to work no longer. I send the package to our 2nd tier only.

According to them though, this is replicable with ACF or CPT UI if the post type is set to "Not translatable". Setting the post type to "Translatable - only show translated content" solves this issue - as long there are not any translated posts.

Can you try this, would it work?

July 18, 2024 at 12:36 pm #15972525

adrienR-5

Hi Bruno,

So doing this make my archive page not offering other translations in the dropdown.
But those links are still accessible and set to "index", example here: hidden link

How can I prevent them to be generated and accessible?

Also how do I translate those archive page for the languages I need to translate?

July 19, 2024 at 2:14 pm #15978863

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

Can you tell us if the issue happens if Metabox AIO is disabled?

July 20, 2024 at 7:27 am #15981005

adrienR-5

Hey Bruno, I'm wondering if this is a real question, if I disable Metabox AIO all my CPTs are gone so obviously everything related to CPTs will be error 404, I don't know how it can help us

July 22, 2024 at 9:10 am #15985727

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

I apologize, there was some confusion on the details of this ticket. I will get back to you.

July 22, 2024 at 9:31 am #15985827

adrienR-5

Hello Bruno,

No worries let me know please

July 23, 2024 at 7:30 am #15990893

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

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

Can you please try the following:

1. Add the custom code provided to your theme's `functions.php` file. This code prevents the registration of the Custom Post Type (CPT) on non-default languages by removing the `mb_cpt_load` action before it is triggered.

   function remove_mb_cpt_load_action() {
        $my_current_lang = apply_filters( 'wpml_current_language', null );
        $my_default_lang = apply_filters( 'wpml_default_language', null );
        if ( $my_current_lang != $my_default_lang ) {
            remove_action( 'init', 'mb_cpt_load', 0 );
        }
    }

    add_action( 'init', 'remove_mb_cpt_load_action', -1 );

2. Verify that the Custom Post Type (CPT) does not register in non-default languages by visiting your site in different language settings. If the CPT appears only in the default language, the issue should be resolved.

July 23, 2024 at 12:29 pm #15992403

adrienR-5

Hello Bruno,

Your code snippet was a bit too hard, since it disable all CPTs for the others languages, which I don't want.

I created this code snippet, which just disable the archive slug for some CPTs in all other languages than the default, that's exactly what I wanted, to avoid unwanted archive slug for other languages.

```
function sws_remove_cpt_archive_for_other_languages() {
// Get the current and default language
$current_language = apply_filters( 'wpml_current_language', null );
$default_language = apply_filters( 'wpml_default_language', null );

// Check if the current language is not the default language
if ( $current_language !== $default_language ) {
// Remove the archive page for specified CPTs
add_action('template_redirect', 'sws_disable_cpt_archive');
}
}

function sws_disable_cpt_archive() {
// Array of CPTs for which to disable the archive page
$cpts_to_disable = array(
'cpt1',
'cpt2',
'cpt3',
); // Examples, replace by yours

// Check if the current request is for any of the specified CPTs' archive page
foreach ($cpts_to_disable as $cpt) {
if (is_post_type_archive($cpt)) {
// Redirect to the homepage of the current language
wp_redirect(apply_filters('wpml_home_url', home_url()));
exit();
}
}
}

add_action( 'init', 'sws_remove_cpt_archive_for_other_languages', -1 );
```

Edit: how do we add code syntax in our replies?