Back to Community

How to Exclude Specific URLs from Your Rank Math Sitemap

16 threads Sep 16, 2025 PluginRank math seo

Content

Many WordPress users rely on the Rank Math SEO plugin to manage their XML sitemaps. A common challenge is preventing certain pages, posts, or images from appearing in these sitemaps, even after using the plugin's standard settings. This guide explains why this happens and provides the most effective solutions.

Why URLs Might Still Appear in Your Sitemap

While Rank Math provides user-friendly options to exclude content from the sitemap, certain scenarios require a more direct approach. The standard "Exclude Posts" field in the sitemap settings works by post ID. However, issues can arise with:

  • Dynamically generated pages (e.g., WooCommerce checkout pages).
  • Content that is not a standard post or page.
  • Specific images within posts that you want to remove.
  • Taxonomy archive pages.

In these cases, a programmatic solution using WordPress filters is often necessary to gain precise control.

Solution: Using the `rank_math/sitemap/entry` Filter

The most powerful and common method for excluding specific URLs is to use the `rank_math/sitemap/entry` filter in your theme's functions.php file or a custom code snippet plugin.

1. Exclude by Exact URL

This code snippet checks the URL of each item being added to the sitemap and excludes it if it matches one in your list.

add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
    $excluded_urls = [
        'https://yoursite.com/checkout/',
        'https://yoursite.com/my-account/',
        'https://yoursite.com/private-page/'
    ];

    if ( in_array( $url['loc'], $excluded_urls ) ) {
        return false;
    }
    return $url;
}, 10, 3 );

2. Exclude Posts by Taxonomy Term

To exclude all posts that have a specific tag or category, you can use a more advanced check within the same filter.

add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
    // Only run for posts
    if ( 'post' === $type && $object instanceof WP_Post ) {
        // Check if the post has the term 'testimonials' in the 'success_types' taxonomy
        if ( has_term( 'testimonials', 'success_types', $object ) ) {
            return false; // Exclude from sitemap
        }
    }
    return $url;
}, 10, 3 );

3. Exclude Specific Images from the Sitemap

If you need to remove a specific image (e.g., a preloader placeholder) that appears in multiple posts, use the `rank_math/sitemap/urlimages` filter.

add_filter( 'rank_math/sitemap/urlimages', function( $images, $post_id ) {
    foreach ( $images as $key => $image ) {
        // Replace with the actual URL of the image you want to remove
        if ( $image['src'] === "https://yoursite.com/wp-content/uploads/placeholder.jpg" ) {
            unset( $images[$key] ); // Remove the image from the array
        }
    }
    return $images;
}, 10, 2 );

Important Considerations

  • Clear Your Cache: After adding any code, clear your WordPress cache and any server-side caching (like LiteSpeed or CDN) to see the changes reflected in your sitemap immediately.
  • Use a Code Snippet Plugin: To avoid losing custom code when updating your theme, it is recommended to add these filters using a dedicated plugin like Code Snippets.
  • Test Thoroughly: Always test changes on a staging site first. After implementation, use a tool like Google's Search Console or the Screaming Frog SEO Spider to fetch and review your sitemap and ensure the URLs have been successfully excluded.

By using these targeted code solutions, you can achieve much finer control over your Rank Math sitemap content, ensuring search engines only crawl the pages you want them to see.

Related Support Threads Support