WPSurfer.com

How to Customize WordPress Native Sitemaps

Published on December 28, 2022 | Updated on November 3, 2024

A sitemap is a file that lists the pages on a website. It is usually written in XML format and is used to help search engines discover the posts, pages, categories, and images on a website and understand how they are organized.

Sitemaps can be used to provide additional information about each page, such as when it was last updated and how often it changes. This can help search engines crawl the site more intelligently.

WordPress provides native support for sitemaps since WordPress 5.5. By default, WordPress creates an XML sitemap of your website’s content that can be accessed at the URL “https:yourdomain.com/wp-sitemap.xml



Code Snippets for Native Sitemaps

By default, WordPress will create a sitemap for your posts, pages, categories, and users.

I redirect my category pages to pages so I don’t need tags or categories.

In the past, I didn’t index my author page but I believe it is important now to improve E.E.A.T


How to Disable Author Sitemap

This is the code you need if you want to disable the author sitemap:

// disable users sitemap
function shapeSpace_disable_sitemap_users($provider, $name) {
	
	return ($name == 'users') ? false : $provider;
	
}
add_filter('wp_sitemaps_add_provider', 'shapeSpace_disable_sitemap_users', 10, 2);

Code Snippet to Disable Tags

This is the code that you have to add to your functions.php file to disable tags

// disable taxonomy sitemap
function shapeSpace_disable_sitemap_taxonomy($taxonomies) {
	
	unset($taxonomies['post_tag']); // can be post_tag, category, post_format, or any taxonomy
	
	return $taxonomies;
	
}
add_filter('wp_sitemaps_taxonomies', 'shapeSpace_disable_sitemap_taxonomy');

Code Snippet to Disable Categories

This is the one to disable categories from the sitemap

// disable categories sitemap
function shapeSpace_disable_sitemap_taxonomy($taxonomies) {
	
	unset($taxonomies['category']); // can be post_tag, category, post_format, or any taxonomy
	
	return $taxonomies;
	
}
add_filter('wp_sitemaps_taxonomies', 'shapeSpace_disable_sitemap_taxonomy');

How to Add the Last Modified Date to the Sitemaps

One of the reasons I had for using a plugin to handle the sitemap was the last modified date.

Add this code to your functions.php file and you will have that on your site’s native sitemaps.

//Date of the last modification in WP sitemap
add_filter(
    'wp_sitemaps_posts_entry',
    function( $entry, $post ) {
        $entry['lastmod'] = $post->post_modified_gmt;
        return $entry;
    },
    10,
    2
);

You don’t need to add the previous code since the last modified date was already to WordPress Native Sitemaps since WordPress 6.5

Now if you feel the opposite way about adding the last modified date to your posts, you can use this PHP snippet.

add_filter( 'wp_sitemaps_posts_entry', function( $entry ) {
    if ( isset( $entry['lastmod'] ) ) {
        unset( $entry['lastmod'] );
    }
    return $entry;
});

If you are wondering why someone would remove the last modified date from their website sitemaps.

Remember that Google has been smashing everybody

Smash Everybody

Google doesn’t seem to like the fact that you update a post without making substantial changes to it, so why would you let them know?

So you gotta lock or freeze the modified date or use a custom one.

SEO: Freezing or Locking the Last Modified Date of your Posts

SEO: Freezing or Locking the Last Modified Date of your Posts

If you are not a big fan of using SEO Plugin or adding a specific plugin to have this feature, you probably want to do this via a mu-plugin

Since I am using a custom one, I decided to remove the last mod feature.


How to Exclude Individual Posts

This is the code to exclude individual posts

add_filter(
    'wp_sitemaps_posts_query_args',
    function( $args, $post_type ) {
        if ( 'post' !== $post_type ) {
            return $args;
        }
 
        $args['post__not_in'] = isset( $args['post__not_in'] ) ? $args['post__not_in'] : array();
        $args['post__not_in'][] = 123; // 123 is the ID of the post to exclude.
        return $args;
    },
    10,
    2
);

Sitemap Issues

When taking care and control of your sitemaps, don’t let any plugins or mu-plugins move these url from a non-secure to a secure connection

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap>
https://www.w3.org/1999/XSL/Transform

If you do that, that will break the style of your sitemaps.

Your sitemaps will still be read by Google Crawlers and Bots but won’t look any good if you want to have a closer look at it.


Manuel Campos

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.