Yoast WordPress SEO: Sitemap for each language

The WordPress SEO plugin by Yoast is very popular (more than 1 million active installs), including the functionality to add XML sitemaps to your website.

If you have a multi-lingual website, it’s not so clear how to get separate sitemaps for each language. If you use WPML, out of the box it is only possible if you have different subdomains (e.g. en.mywebsite.com and fr.mywebsite.com) and in the WPML language settings select ‘a different domain for each language’.

But what if you have different languages in subfolders (e.g. mywebsite.com/en/ and mywebsite.com/fr/)?

Well, it’s not so difficult at all. Add the following code into your functions.php file of your WordPress theme:


if (isset($sitepress)) add_filter('wpseo_posts_join', 'sitemap_per_language', 10, 2);
function sitemap_per_language($join, $type) {
    global $wpdb, $sitepress;
    $lang = $sitepress->get_current_language();
    return " JOIN " . $wpdb->prefix . "icl_translations ON element_id = ID AND element_type = 'post_$type' AND language_code = '$lang'";
}

The XML sitemap for your default language is still in the same place (mywebsite.com/sitemap_index.xml). Your other languages don’t have an ‘index.xml’, but separate sitemaps, like ‘mywebsite.com/fr/post-sitemap.xml‘ and ‘mywebsite.com/fr/page-sitemap.xml‘.

So, if you want to submit your sitemaps in Google Webmaster Tools you have to add ‘sitemap_index.xml’ for your default language, but for your other languages you have to add multiple XML sitemaps as described above.

This comes in handy when you want to set different geotargets for your languages.