How to Exclude Specific URLs from Your Rank Math Sitemap
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
-
Comment-page in Sitemaphttps://wordpress.org/support/topic/comment-page-in-sitemap/
-
Exclude post from sitemap (php code)https://wordpress.org/support/topic/exclude-post-from-sitemap-php-code/
-
Exclude posts with taghttps://wordpress.org/support/topic/exclude-posts-with-tag-2/
-
WooCommerce My Account and Checkout pages can not be excluded from sitemaphttps://wordpress.org/support/topic/woocommerce-my-account-and-checkout-pages-can-not-be-excluded-from-sitemap/
-
Priority and Frequencyhttps://wordpress.org/support/topic/priority-and-frequency/
-
Add pagination to sitemap to crawl themhttps://wordpress.org/support/topic/add-pagination-to-sitemap-to-crawl-them/
-
Number of posts/products in the sitemaphttps://wordpress.org/support/topic/number-of-posts-products-in-the-sitemap/
-
`sitemap/entry` inconsistent interface definitionhttps://wordpress.org/support/topic/sitemap-entry-inconsistent-interface-definition/
-
How to include /post/2/ and /post/3/ in XML sitemap?https://wordpress.org/support/topic/how-to-include-post-2-and-post-3-in-xml-sitemap/
-
Would like to inside sitemap URL within sitemap_index.xmlhttps://wordpress.org/support/topic/would-like-to-inside-sitemap-url-within-sitemap_index-xml/
-
How to exclude the main taxonomy page from sitemap ?https://wordpress.org/support/topic/how-to-exclude-the-main-taxonomy-page-from-sitemap/
-
Exclude preloader images from sitemaphttps://wordpress.org/support/topic/exclude-preloader-images-from-sitemap/
-
How to allow only granted crawler fetch xml sitemap?https://wordpress.org/support/topic/how-to-allow-only-granted-crawler-fetch-xml-sitemap/
-
Suggestion: Allow us to add urls to sitemaphttps://wordpress.org/support/topic/suggestion-allow-us-to-add-urls-to-sitemap/
-
Category sitemap – not creating second page in sitemaphttps://wordpress.org/support/topic/category-sitemap-not-creating-second-page-in-sitemap/
-
Restructuring the sitemaphttps://wordpress.org/support/topic/restructuring-the-sitemap/