sitemap filter in

#295848
  • Resolved Joey
    Rank Math free

    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;
    }
    });

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

    Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.

    That filter is not to be used to set the URLs in bulk like so because as is mentioned in the filter documentation it requires an absolute local URL to be passed to the $url variable.

    Here’s a working example:

    
    add_filter( 'rank_math/sitemap/xml_post_url', function( $url, $post) {
        if($post->ID == '869') {
            $url = 'https://rankmath.local/elementor-869-rm-filter/';
        }
        return $url;
    }, 10, 2 );
    

    ​​​​​​​Hope this helps clarify your doubts.

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

    Joey
    Rank Math free

    Is there any way to make RankMath use the correct URL?

    Nigel
    Rank Math business

    Hello,

    You can try the filter to change the sitemap URL base: https://rankmath.com/kb/filters-hooks-api-developer/#change-sitemap-base

    You can also check the code snippets on the rest of the linked page.

    Hope that helps. Let us know if you have questions. We are here to help.

    Joey
    Rank Math free

    Hey Nigal. It looks like the filter you suggested is for editing the URL of the xml sitemap and not the URLs of the posts contained in the sitemap.

    Nigel
    Rank Math business

    Hello,

    Thank you for your patience and for the clarification.

    If I understand correctly, you want to replace https://www.biggerpockets.com/blog/glossary with https://www.biggerpockets.com/glossary

    The filter to change a sitemap entry before it’s added to the sitemap should work. The modified snippet below should work for you:

    
    /**
     * Filter URL entry before it gets added to the sitemap.
     *
     * @param array  $url  Array of URL parts.
     * @param string $type URL type. Can be user, post or term.
     * @param object $object Data object for the URL.
     */
    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
    	
    	if ( "https://www.biggerpockets.com/blog/glossary" == $url['loc'] ){
    		$url["loc"] = "https://www.biggerpockets.com/glossary";
    	}	
    	return $url;
    }, 10, 3 );
    

    Hope that helps.

    Joey
    Rank Math free

    What does the $url data look like? Is $url[“loc”] the entire URL for an entry in the sitemap?

    Joey
    Rank Math free

    What does the $url data look like? Is $url[“loc”] the entire URL for an entry in the sitemap?

    
    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
    	if (strpos($url["loc"], '/blog/glossary') !== false) {
    		$url["loc"] = str_replace( '/blog', '', $url );
    		return $url;
    	}
    	else {
    		return $url;
    	}
    }
    

    However, the following code sample mostly works:

    
    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
    	$url = str_replace( '/blog/glossary', '/glossary', $url );
    	return $url;
    }, 10, 3 );
    

    I say mostly works, because it correctly updates all of the CPT URLs. However, it does not update the archive URL. I noticed that there’s an archive URL sitemap filter. Maybe I’ll have to check that out.

    I am still curious to your use of $url[‘loc’] and why that doesn’t work for me.

    Nigel
    Rank Math business

    In the filter on rank_math/sitemap/entry, $url is an array for each row of the sitemap. $url contains the location, images, and last modified date ‘loc’, ‘images’, ‘mod’ respectively. $url[‘loc’] is the entire URL as it appears in the sitemap.

    The filter seems to only work when a static posts archive page is set under WordPress > Settings > Reading > ‘Your homepage displays’ and setting it to static page.

    Hope that helps.

    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 8 replies - 1 through 8 (of 8 total)

The ticket ‘sitemap filter in’ is closed to new replies.