Drupal 9 : How to exclude node from sitemap.xml
Sometimes we need to write some code to exclude nodes from the sitemap.xml based on custom conditions,
The XML sitemap module provides a hook to do this
function mymodule_xmlsitemap_link_alter(array &$link, array $context) {
if (own_condition) {
// status = 0 exclude the item in the sitemap.
// access = 0 make the element non-accessible by the anonymous user.
$link['status'] = 0;
$link['access'] = 0;
}
else {
$link['status'] = 1;
$link['access'] = 1;
}
}
}
}