How to Customize WordPress Native Sitemaps

Manuel campos

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 does provide 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 post, pages, categories, and users.

I want posts, and pages to be indexed.

I redirect my category pages to pages and I don’t want my author page to be indexed.

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);

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');

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
);

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
);

More Code Snippets

There are more code snippets where this came from.

  1. Add Reusable Blocks to Admin Menu
  2. How to Stop WordPress From Guessing URLs
  3. How to Apply CSS Conditionally
  4. GeneratePress Hooks: Simple Guide
  5. Adding a Class to Images, Videos, and Iframes
  6. How To Remove the Category From the WordPress URL
  7. How to Delay The Execution of JavaScript without a plugin
Wanna buy me a Coffee?
JM
About me
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.
Linktree
Care for Site Speed?
I think you can’t go wrong with Cloudways, CloudFlare, and Perfmatters.
Perfmatters Cloudways

WPSurfer.com

Resources to host, secure, speed up, rank, and monetize your WordPress sites.
Plugins
Services
Connect
LinkedIn
Facebook
LinkTree