SEO PHP Beginner

Remove Post Types and Taxonomies From the Sitemap

Keep content types you don't want indexed, such as tags or a private custom post type, out of WordPress's XML sitemap.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 11 lines
// Post types to leave out of /wp-sitemap.xml.
add_filter( 'wp_sitemaps_post_types', function ( array $post_types ): array {
	unset( $post_types['portfolio'] );
	return $post_types;
} );

// Taxonomies to leave out.
add_filter( 'wp_sitemaps_taxonomies', function ( array $taxonomies ): array {
	unset( $taxonomies['post_tag'] );
	return $taxonomies;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress’s sitemap lists every public post type and taxonomy. If tag pages are thin or a custom post type isn’t meant for search, leaving them out keeps search engines focused on the pages that matter.

How it works

wp_sitemaps_post_types and wp_sitemaps_taxonomies return the post types and taxonomies included in the sitemap, keyed by name. Removing a key takes its sitemap out of the index.

Things to watch

  • Removing something from the sitemap doesn’t stop it being indexed if search engines find it through links. Add noindex as well if you want it out of search results.
  • Use the internal names, such as post_tag rather than “Tags”. You can see them in the URL when editing.

How to test it

Open yoursite.com/wp-sitemap.xml. The removed types should no longer be listed.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only