Hello,
We apologize for the issue you are facing with incorrect entries for translated homepages in the sitemap. This is indeed a common problem, and we appreciate you bringing it to our attention.
Currently, to resolve this issue, you need to manually add the provided code snippet to your theme’s rank-math.php file. Unfortunately, there isn’t an automatic update that can be applied across all your sites to fix this specific issue at once. Here’s the code you need to add:
if ( 'page' === get_option( 'show_on_front' ) ) {
add_filter( 'rank_math/sitemap/entry', 'compsupp_5514_filter_sitemap_home_page_entry', 10, 3 );
}
function compsupp_5514_filter_sitemap_home_page_entry( $url, $type, $post ) {
global $sitepress;
static $wpml_home_urls = null;
static $frontpage_translations = null;
static $default_frontpage_id = null;
if ( ! $wpml_home_urls ) {
$active_languages = $sitepress->get_active_languages();
$wpml_home_urls = [];
foreach ( $active_languages as $code => $language ) {
$wpml_home_urls[ $code ] = $sitepress->language_url( $code );
}
$default_frontpage_id = get_option( 'page_on_front' );
$frontpage_trid = $sitepress->get_element_trid( $default_frontpage_id, 'post_page' );
$frontpage_translations = array_map( WPMLFPObj::prop( 'element_id' ), $sitepress->get_element_translations( $frontpage_trid, 'post_page' ) );
}
if ( in_array( $post->ID, $frontpage_translations ) && $post->ID !== $default_frontpage_id ) {
$language_code = array_search( $post->ID, $frontpage_translations );
$url['loc'] = $wpml_home_urls[ $language_code ];
}
return $url;
}
Please ensure to take a backup of your website’s file and database before making any changes to the code.
If you have any further questions or need assistance with this process, please feel free to reply to this ticket, and a human agent from the Rank Math Support team will assist you further. Thank you for your understanding!
Sources:
Multilingual SEO with Rank Math SEO & WPML » Rank Math
This is an automated reply from Rank Math Support Bot. If you need further assistance, please reply to this topic and a human support agent will reply to you shortly.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.