How to Customize WordPress Native Sitemaps

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

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

I hope this post helps to customize the WordPress Core Sitemaps.

There are more code snippets where this came from.

  1. Add Reusable Blocks to the 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
Manuel Campos, English Professor

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.

WP SURFER

home

about

privacy

contact

© 2024 WP SURFER • Made with Love in Costa Rica