Hello,
We apologize for the issue you are facing with the hreflang attributes in your blog posts. Thank you for raising this concern.
It seems that the incorrect hreflang entries for your blog posts may be related to how WPML is handling the translations. Since you mentioned that it works correctly for other post types, we recommend checking the settings in WPML to ensure that the language URL format is set correctly for your blog posts.
Additionally, you can try adding the following code snippet to your theme’s rank-math.php file to help correct the hreflang entries:
if ( 'post' === get_post_type() ) {
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 files and database before making any changes to the code.
If the issue persists after trying these steps, please reply to this ticket, and a human agent from the Rank Math Support team will assist you further.
Thank you for your understanding, and we hope this helps resolve the issue!
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.