Hello,
We apologize for the issue you are facing with the URL association. It seems like you are encountering a common problem where the sitemap entries for translated homepages are not configured correctly. To resolve this, you can add a specific piece of code to your theme’s rank-math.php
file. This code will help ensure that the correct URLs are generated for your translated pages in the sitemap.
Here’s the code you can 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;
}
Note: We recommend that you take a backup of your website’s file and database before making any changes to the code.
If you continue to experience issues or if this does not resolve your problem, please reply to this ticket with your site URL so that a human agent from our support team can assist you further.
Thank you for raising this concern!
Sources:
Multilingual SEO with Rank Math SEO & WPML » Rank Math
Perfect, I’ve added the requested code, now how do I clear the sitemap cache so that a new one is generated?
Hello,
Please follow these steps to flush your sitemap cache:
1. Flush the Sitemap cache by following this video screencast:
https://i.rankmath.com/pipRDp
2. Exclude the Sitemap files of the Rank Math plugin in your caching plugin. The cache could be via a plugin or from the server. For plugins or Cloudflare, please follow this article:
https://rankmath.com/kb/exclude-sitemaps-from-caching/
3. Disable the transient cache for the sitemap by applying this filter. This will make sure that your XML sitemap always reflects the latest changes on your site:
/**
* Filter if XML sitemap transient cache is enabled.
*
* @param boolean $unsigned Enable cache or not, defaults to true
*/
add_filter( 'rank_math/sitemap/enable_caching', '__return_false');
If you’re not sure how to add this code, you can follow this guide:
https://rankmath.com/kb/wordpress-hooks-actions-filters/
Looking forward to helping you.
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.