Exclude Specific Posts From the Rank Math Sitemap
Leaves the posts and pages you list out of Rank Math's XML sitemaps without changing their robots settings.
Opens WPIDE on
Code
// IDs to leave out.
$wpide_rm_sitemap_exclude = array( 12, 34 );
add_filter( 'rank_math/sitemap/entry', function ( $url, $type, $object ) use ( $wpide_rm_sitemap_exclude ) {
if ( 'post' === $type && $object instanceof WP_Post && in_array( (int) $object->ID, array_map( 'intval', $wpide_rm_sitemap_exclude ), true ) ) {
return false;
}
return $url;
}, 10, 3 );
Notes
Rank Math caches sitemaps; clear the cache or update a post to see the change.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.