How to exclude post types from wp-sitemap.xml in WordPress

Looking to exclude post types or custom post types from the wp-sitemap.xml file in WordPress? Here’s a quick guide on how to achieve this.

Removing a post type from the wp-sitemap.xml file

In order to remove any post types from the wp-sitemap.xml file, you’ll need to add the following code into your WordPress theme’s functions.php file.

function remove_post_type_from_wp_sitemap( $post_types ) {
     unset( $post_types['page'] ); // Change page to any post type name
     return $post_types;
}

add_filter( 'wp_sitemaps_post_types', 'remove_post_type_from_wp_sitemap' );