Remove URLs from sitemap with a hook

#438747
  • Resolved Sam Underwood
    Rank Math pro

    I’m using this to add a noindex tag to empty posts:


    add_filter( 'rank_math/frontend/robots', function( $robots ) {
    if(is_singular('site') && !has_blocks() ) {
    if(isset($robots['index'])){
    $robots['index'] = 'noindex';
    }
    }

    return $robots;
    });

    But the noindex URLs are still within XML sitemaps. How can I also remove these URLs from XML sitemaps if the post content is empty?

Viewing 5 replies - 1 through 5 (of 5 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.

    The filter you are using runs only when the page is loaded on a browser, and because of that, the URLs will still be set to index when the sitemap functions run.

    It’s correct that the noindex URLs would not be included, but this is a very particular case.

    You may need to use and customize this filter to remove the URLs from the sitemap as well: https://rankmath.com/kb/filters-hooks-api-developer/#filter-sitemap-item

    I hope that helps.

    Thank you.

    Could you provide an example of how to use that hook to do that please?

    Anas
    Rank Math business

    Hello,

    Here’s an example that removes any URL containing the word review in the URL:

    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
        if( strpos( $url['loc'], 'review' ) && $type == 'post' ){
            return false;	
        }
        return $url;
    }, 10, 3 );
    

    Hope this helps solve your issues.

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

    Thanks!

    Hello,

    We are super happy that this resolved your issue.

    If you have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

You must be logged in to reply to this ticket.