How to Modify WordPress Default Sitemap

With the introduction of WordPress 5.5 a sorely needed feature has finally been added. A default XML Sitemap. A great addition for me since the only reason I use an SEO plugin is to add the sitemap which Google Search Console aka Webmasters Tool highly recommends.

Your new WordPress XML Sitemap should be found like in the URL below:

yoursite.com/wp-sitemap.xml

But after submitting your sitemap you may end up getting Coverage Errors on Google Search Console like the ones I have, shown on the image below:

google console noindex errors

You can see about 100 items listed with an error type of “Submitted URL marked noindex”. This is because Taxonomies are marked by WordPress as ‘noindex’ pages as they are not really meant to be indexed. Taxonomies are also known as Categories and Tags. They would just cause lots of redundancies when added. So we’ll be removing them with the code below.

Remove Unneeded Sitemaps From wp-sitemap.xml

What the code below does is remove the sitemap pages for taxonomies and users that are linked in wp-sitemap.xml. Users do not need to be indexed in most cases, especially on WordPress sites with fewer than 5 users. Since most blogs are single user sites they can be safely removed.

As usual this code should be added on a child theme’s functions.php file.

function custom_sitemap_modify( $provider, $name ) {

	if ( 'users' === $name || 'taxonomies' === $name ) {
		return false;
	}

	return $provider;
}

add_filter( 'wp_sitemaps_add_provider', 'custom_sitemap_modify', 10, 2 );

With this Google would only read and index Posts and Pages from your site.

The code below is for only disabling specific pages. In this sample code a page with an ID of ‘123’ will be excluded from the sitemap.

function custom_sitemap_disable_specific_page( $args, $post_type ) {
	
	if ( 'page' !== $post_type ) return $args;
	
	$args['post__not_in'] = isset($args['post__not_in']) ? $args['post__not_in'] : array();
	$args['post__not_in'][] = 123;
	
	return $args;
}

add_filter( 'wp_sitemaps_posts_query_args', 'custom_sitemap_disable_specific_page', 10, 2 );

Leave a Reply

Please write your comments in english. I delete anything that even remotely resembles spam.

Note: When posting code, enclose it in pre and code tags.
e.g. <pre><code> Add code here </code></pre>