-
I’m using the sitemap/xml_post_url filter to replace part of the URL for a custom post type sitemap. Here’s the sitemap I’m working with: https://www.biggerpockets.com/blog/glossary-sitemap.xml
I have the following code samples in the rank-math.php file in the root of my theme directory. After adding the below samples I cleared the permalink cache and updated the sitemap settings in RankMath.
In my first attempt, I wanted to check the $url for “/blog/glossary” and if it exists, I’d remove “/blog”. This code sample did not work.
add_filter( 'rank_math/sitemap/xml_post_url', function( $url, $post){
if (strpos($url, '/blog/glossary') !== false) {
$url = str_replace( '/blog', '', $url );
return $url;
}
else {
return $url;
}
}, 10, 2 );
After multiple failed attempts, I tried simplifying my code and attempted a simple string replace. This code sample also did not work.
add_filter( 'rank_math/sitemap/xml_post_url', function( $url, $post){
$url = str_replace( '/blog/glossary', '/glossary', $url );
return $url;
}, 10, 2 );
I am successfully using the frontend/canonical filter. So I know I’m not totally misunderstanding the use of these filters.
add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
if ( 'glossary' == get_post_type() ) {
return str_replace("/blog/", "/", $canonical);
} else {
return $canonical;
}
});
The ticket ‘sitemap filter in’ is closed to new replies.