If you use the Rank Math SEO plugin to control which post types appear in your XML sitemap, you may have noticed the “Include in Sitemap” option under Rank Math SEO → Sitemap Settings → Post Types.
However, there are cases where you need to override this setting programmatically – for example, when you want to exclude a specific post type dynamically or based on certain conditions. In those cases, you can use the rank_math/sitemap/exclude_post_type filter.
The filter accepts two parameters: $exclude (defaults to false) and $type (the post type name). Returning true will exclude that post type from the sitemap.
/**
* Filter decision if post type is excluded from the XML sitemap.
*
* @param bool $exclude Default false.
* @param string $type Post type name.
*/
add_filter( 'rank_math/sitemap/exclude_post_type', function( $exclude, $type ){
if ( 'snippet' === $type ) {
return true;
}
return $exclude;
}, 10, 2 );In the example above, the snippet post type is excluded from the sitemap. Replace 'snippet' with the post type name relevant to your site.