This was needed on the Riga website where products were translated for foreign Amazon webshops while the translations should not be available on their own website. There are two parts to prevent these pages from being indexed:
- Remove translated pages from the sitemap created by Yoast.
- Prevent translated pages from being indexed.
1. Remove translated pages from the sitemap
Add the following code to functions.php of the child theme.
This only works for normal post types, not categories etc. You also need to do something to noindex,follows them on the page itself.
// START Set translations to noindex. This removes them from the sitemap only
// Special pages (shop), categories, attributes remain in the sitemap etc
// Also some code added to .htindex
/**
*
* @param mixed $value
* @param int $object_id
* @param string $meta_key
*
* @return mixed
*
*/
function hws_wpml_set_translaton_noindex( $value, $object_id, $meta_key ) {
if ( '' === $meta_key ) {
$args = [
'element_id' => $object_id,
'element_type' => get_post_type( $object_id ),
];
$postLang = apply_filters( 'wpml_element_language_code', '', $args );
$RemoveLanguages = array('de', 'en', 'es', 'fr', 'it');
if (in_array($postLang, $RemoveLanguages)){
remove_filter( 'get_post_metadata', 'hws_wpml_set_translaton_noindex' );
$metaData = get_post_meta( $object_id );
add_filter( 'get_post_metadata', 'hws_wpml_set_translaton_noindex', 10, 3 );
$metaData['_yoast_wpseo_meta-robots-noindex'] = [ '1' ];
return $metaData;
}
}
return $value;
}
add_filter( 'get_post_metadata', 'hws_wpml_set_translaton_noindex', 10, 3 );
// END Set translations to noindex. This removes them from the sitemap only
Code language: PHP (php)
2. Prevent translated pages from being indexed
Add the following to the .htaccess file
### START set no index tags ###
# Also some code added to functions.php
# On DE pages
<ifModule mod_headers.c>
<If "%{REQUEST_URI} =~ m#(\/de\/).*#i">
Header set X-Robots-Tag "noindex, follow"
</If>
</IfModule>
# On EN pages
<ifModule mod_headers.c>
<If "%{REQUEST_URI} =~ m#(\/en\/).*#i">
Header set X-Robots-Tag "noindex, follow"
</If>
</IfModule>
# On FR pages
<ifModule mod_headers.c>
<If "%{REQUEST_URI} =~ m#(\/es\/).*#i">
Header set X-Robots-Tag "noindex, follow"
</If>
</IfModule>
# On SP pages
<ifModule mod_headers.c>
<If "%{REQUEST_URI} =~ m#(\/fr\/).*#i">
Header set X-Robots-Tag "noindex, follow"
</If>
</IfModule>
# On IT pages
<ifModule mod_headers.c>
<If "%{REQUEST_URI} =~ m#(\/it\/).*#i">
Header set X-Robots-Tag "noindex, follow"
</If>
</IfModule>
### START set no index tags ###
Code language: HTML, XML (xml)