exclude wp multilang translated version of posts from sitemap

#309753
  • Resolved Hasan
    Rank Math free

    Hi
    We are using WP MultipLang to translate our posts,
    wp multilang does translation using the same post-id ($post->ID) means all the 3 versions of posts shares same post-id,
    say, we have one post that has 3 translations,
    url like below, but same postid for all 3 translations.
    domain.com/pt/post-slug
    domain.com/en/post-slug
    domain.com/es/post-slug

    so if we add canonical url in post edit screen, its’ excluding all 3 versions of translations from sitemap and
    if we add canonical/noindex using wp_hea hook, it’s not working and add all 3 versions of translations in the sitemap.
    but we need to exclude 2 versions of translation and show one ( say /pt/ ) language in sitemap.
    Tried with rankmath filter “7. Filter to change/remove Post URL, plugin uses in the sitemap.”
    which has no effect in the sitemap.
    Can you please give me an example of code to use this filter?
    Can you guide us to exclude translated posts and show only one translation in sitemap ?

    Thanks in advance.
    Ibnul

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    To exclude posts of a specific language from the sitemap, you would need to create a custom function to filter posts of that specific language. The function would include a conditional for the post language which you can get from your language plugin documentation.

    Here’s a filter you can customize for excluding the posts from your sitemap:

    add_filter( ‘rank_math/sitemap/xml_post_url’, function( $url, $post){
    if(strpos($url, '/es/')!==false){
    return false;
    }
    return $url;
    }, 10, 2 );

    I hope that helps.

    Thank you.

    Hasan
    Rank Math free

    Hello,
    Thanks for your reply and sorry for a late reply.
    Actually, my client was trying with another ticket and was waiting to get something but no luck after getting back and forth in several reply.

    Then I’ve been trying with your code but it’s not working either.
    I’m a developer on behalf of the client and I know what is the issue.
    Let me explain please :
    Site is with WP MultiLang plugin to translate in ​3 languages : [en], [pt], [es] and default is [pt]

    Now when sitemap is generating with filter you provided, it’s not working and showing all 3 languages as below but it supposed to remove ‘/es/’ posts as per the filter code. but it’s not removing …
    https://snipboard.io/dz2kKr.jpg
    because i see variable “$url” is getting only the /pt/… link and not the /es/… link
    I got this printing the variable.
    Again when I’m modifying the filter to search for /pt/

    
    add_filter( 'rank_math/sitemap/xml_post_url', function( $url, $post){
    	//print_r('<pre>'. $url.'</pre>');
    	if(strpos($url, '/pt/')!==false){
    		//print_r('<pre>Found</pre>');
    		return false;
    	}
    	else{
     		//print_r('<pre>Not Found</pre>');
     	}
    	return $url;
    }, 10, 2 );
    

    it’s working and removing all the links please check
    https://snipboard.io/sHW8Vy.jpg

    WP Multilang works like it uses same post id for all 3 translated version and same url slug. but just adding /es/, /en/, /pt/, before the slug.

    So would you please check my issue carefully and kindly bring me a better solution?
    Even if it’s possible to do with another sitemap plugin would be great.

    Thanks in advance.
    ~ Ibnul

    Hello,

    Since that plugin loads, all the translations on the same ID our plugin is fetching the URLs via the ID and returning them in the sitemap.

    Because of this, the filters we have available for the sitemap generation won’t work as expected for this case.

    The other alternative might be to create a custom sitemap, with your custom logic in there, and for that you can refer to this article here: https://rankmath.com/kb/custom-sitemaps/

    Hope this helps clarify your doubts.

    Don’t hesitate to get in touch if you have any other questions.

    Hasan
    Rank Math free

    Hello,
    Thanks for your reply.
    So there is the only way to use custom sitemap, right?
    But as I see it’s something like adding links manually.
    Is it meaning, I’ve to add all the links manually ?

    Thanks in advance.
    Ibnul

    Hello,

    Instead of adding the URLs manually, you can try to create a custom loop to fetch only the posts that need to be in the sitemap.

    Don’t hesitate to get in touch if you have any other questions.

    Hasan
    Rank Math free

    Hi
    I’ve done the steps to add custom-sitemap and added same code as in https://rankmath.com/kb/custom-sitemaps/
    And it’s showing the sitemap as in the link below
    https://staging-capitalinvest.kinsta.cloud/pt/custom-sitemap.xml
    So it’s showing same link thrice because of the 3 language?
    It’s confusing, would you please help to understand what is going on.

    Regards
    Hasan

    Hello,

    This might be caused by the situation that you shared earlier about the same language being on the same post ID.

    Either way, this custom code work falls outside the scope of Rank Math support and we don’t provide code customizations of this nature and complexity.

    Don’t hesitate to get in touch if you have any other questions.

    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.

Viewing 7 replies - 1 through 7 (of 7 total)

The ticket ‘exclude wp multilang translated version of posts from sitemap’ is closed to new replies.