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 23, 2024 at 12:37 pm #15992421

Bruno Kos
Supporter

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

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

You can use "code" tag, so

.

Do you need me to check this snippet with our 2nd tier?

July 23, 2024 at 12:55 pm #15992484

adrienR-5

Sweet thanks, I didn't what I needed to use to higlight code.

<?php 

function my_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', 'my_disable_cpt_archive');
    }
}

function my_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', 'my_remove_cpt_archive_for_other_languages', -1 );

Yes please if you can validate this with your 2nd tier please

July 24, 2024 at 7:44 am #15996513

Bruno Kos
Supporter

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

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

We cannot validate or provide support for custom code, however for this particular case we recommended manually registering the CPTs to avoid such issues.

You can however use your approach if you found no issues after testing.

What we suggest is 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 change ensures `get_posts` will fetch posts only in the specific language by bypassing WPML filters:

$posts = get_posts( [
	'posts_per_page'         => -1,
	'post_status'            => 'publish',
	'post_type'              => 'mb-post-type',
	'no_found_rows'          => true,
	'update_post_meta_cache' => false,
	'update_post_term_cache' => false,
	'suppress_filters'       => false,
] );

Ensure the `mb-post-type` is initially set as "Translated" and not duplicated into the second language. This change may cause issues for non-duplicated or non-translated CPTs of type `mb-post-type`, but duplication should work.

We suggest to contact the plugin author for Meta Box AIO for improvements and support.

July 24, 2024 at 5:03 pm #15999740

adrienR-5

Hello Bruno,

Thanks for the suggestion but I don't want to translate at all my CPTs.

I'll keep my code snippet since it simply avoid having unwanted "translated" archive pages and that's what I wanted.

July 25, 2024 at 6:09 am #16001387

Bruno Kos
Supporter

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

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

If you need any further assistance or have other questions in the future, please don't hesitate to reach out.