wordpress sitemap xml remove link

0
44

Normal sitemap Remove Pages link

Configuring Sitemaps Behavior

Adding Custom Sitemaps

WordPress provides sitemaps for built-in content types like pages and author archives out of the box. If you are developing a plugin that adds custom features beyond those standard ones, or just want to include some custom URLs on your site, it might make sense to add a custom sitemap provider.

To do so, all you need to do is create a custom PHP class that extends the abstract WP_Sitemaps_Provider class in core. Then, you can use the wp_register_sitemap_provider() function to register it. Here’s an example:

1
2
3
4
5
6
7
add_filter(
    'init',
    function() {
        $provider = new Awesome_Plugin_Sitemaps_Provider();
        wp_register_sitemap_provider( 'awesome-plugin', $provider );
    }
);

The provider will be responsible for getting all sitemaps and sitemap entries, as well as determining pagination.

Removing Certain Sitemaps

There are three existing sitemaps providers for WordPress object types like posts, taxonomies, and users. If you want to remove one of them, let’s say the “users” provider, you can leverage the wp_sitemaps_add_provider filter to do so. Here’s an example:

1
2
3
4
5
6
7
8
9
10
11
12
add_filter(
    'wp_sitemaps_add_provider',
    function( $provider, $name ) {
        if ( 'users' === $name ) {
            return false;
        }
        return $provider;
    },
    10,
    2
);

If instead you want to disable sitemap generation for a specific post type or taxonomy, use the wp_sitemaps_post_types or wp_sitemaps_taxonomies filter, respectively.

Example: Disabling sitemaps for the page post type

1
2
3
4
5
6
7
add_filter(
    'wp_sitemaps_post_types',
    function( $post_types ) {
        unset( $post_types['page'] );
        return $post_types;
    }
);

Example: Disabling sitemaps for the post_tag taxonomy

1
2
3
4
5
6
7
add_filter(
    'wp_sitemaps_taxonomies',
    function( $taxonomies ) {
        unset( $taxonomies['post_tag'] );
        return $taxonomies;
    }
);

Adding Additional Tags to Sitemap Entries

The sitemaps protocol specifies a certain set of supported attributes for sitemap entries. Of those, only the URL (loctag is required. All others (e.g. changefreq and priority) are optional tags in the sitemaps protocol and not typically consumed by search engines, which is why WordPress only lists the URL itself. Developers can still add those tags if they really want to.

You can use the wp_sitemaps_posts_entry / wp_sitemaps_users_entry / wp_sitemaps_taxonomies_entry filters to add additional tags like changefreqpriority, or lastmod to single items in the sitemap.

Example: Adding the last modified date for posts

1
2
3
4
5
6
7
8
9
add_filter(
    'wp_sitemaps_posts_entry',
    function( $entry, $post ) {
        $entry['lastmod'] = $post->post_modified_gmt;
        return $entry;
    },
    10,
    2
);

Similarly, you can use the wp_sitemaps_index_entry filter to add lastmod on the sitemap index. Note: the sitemaps protocal does not support on the sitemap index.

Trying to add any unsupported tags will result in a _doing_it_wrong notice.

Excluding a Single Post from the Sitemap

If you are developing a plugin that allows setting specific posts or pages to noindex, it’s a good idea to exclude those from the sitemap too.

The wp_sitemaps_posts_query_args filter can be used to exclude specific posts from the sitemap. Here’s an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
);

Disabling Sitemaps Functionality Completely

If you update the Site Visibility settings in WordPress admin to discourage search engines from indexing your site, sitemaps will be disabled. You can use the wp_sitemaps_enabled filter to override the default behavior.

Here’s an example of how to disable sitemaps completely, no matter what:

1
add_filter( 'wp_sitemaps_enabled', '__return_false' );

Note: Doing that will not remove the rewrite rules used for the sitemaps, as they are needed in order to send appropriate responses when sitemaps are disabled.

Want to know whether sitemaps are currently enabled or not? Use wp_sitemaps_get_server()->sitemaps_enabled().

Image/Video/News Sitemaps

WordPress currently implements and supports the core sitemaps format as defined on sitemaps.org. Sitemap extensions like image, video, and news sitemaps are not covered by this feature, as these are usually only useful for a small number of websites. In future versions of WordPress, filters and hooks may be added to enable adding such functionality. For now this will still be left to plugins to implement.

New Classes and Functions

As of this writing, this is the full list of new classes and functions introduced with this feature.

Functions:

  • wp_sitemaps_get_server – Retrieves the current Sitemaps server instance.
  • wp_get_sitemap_providers – Gets an array of sitemap providers.
  • wp_register_sitemap_provider – Registers a new sitemap provider.
  • wp_sitemaps_get_max_urls – Gets the maximum number of URLs for a sitemap.

Classes:

  • WP_Sitemaps – Main class responsible for setting up rewrites and registering all providers.
  • WP_Sitemaps_Index – Builds the sitemap index page that lists the links to all of the sitemaps.
  • WP_Sitemaps_Provider – Base class for other sitemap providers to extend and contains shared functionality.
  • WP_Sitemaps_Registry – Handles registering sitemap providers.
  • WP_Sitemaps_Renderer – Responsible for rendering Sitemaps data to XML in accordance with sitemap protocol.
  • WP_Sitemaps_Stylesheet – This class provides the XSL stylesheets to style all sitemaps.
  • WP_Sitemaps_Posts – Builds the sitemaps for the ‘post’ object type and its sub types (custom post types).
  • WP_Sitemaps_Taxonomies – Builds the sitemaps for the ‘taxonomy’ object type and its sub types (custom taxonomies).
  • WP_Sitemaps_Users – Builds the sitemaps for the ‘user’ object type.

Available Hooks and Filters

As of this writing, this is the full list of available hooks and filters.

General:

  • wp_sitemaps_enabled – Filters whether XML Sitemaps are enabled or not.
  • wp_sitemaps_max_urls – Filters the maximum number of URLs displayed on a sitemap.
  • wp_sitemaps_init – Fires when initializing sitemaps.
  • wp_sitemaps_index_entry – Filters the sitemap entry for the sitemap index.

Providers:

  • wp_sitemaps_add_provider – Filters the sitemap provider before it is added.
  • wp_sitemaps_post_types – Filters the list of post types to include in the sitemaps.
  • wp_sitemaps_posts_entry – Filters the sitemap entry for an individual post.
  • wp_sitemaps_posts_show_on_front_entry – Filters the sitemap entry for the home page when the ‘show_on_front’ option equals ‘posts’.
  • wp_sitemaps_posts_query_args – Filters the query arguments for post type sitemap queries.
  • wp_sitemaps_posts_pre_url_list – Filters the posts URL list before it is generated (short-circuit).
  • wp_sitemaps_posts_pre_max_num_pages – Filters the max number of pages before it is generated (short-circuit).
  • wp_sitemaps_taxonomies – Filters the list of taxonomies to include in the sitemaps.
  • wp_sitemaps_taxonomies_entry – Filters the sitemap entry for an individual term.
  • wp_sitemaps_taxonomies_query_args – Filters the query arguments for taxonomy terms sitemap queries.
  • wp_sitemaps_taxonomies_pre_url_list – Filters the taxonomies URL list before it is generated (short-circuit).
  • wp_sitemaps_taxonomies_pre_max_num_pages – Filters the max number of pages before it is generated (short-circuit).
  • wp_sitemaps_users_entry – Filters the sitemap entry for an individual user.
  • wp_sitemaps_users_query_args – Filters the query arguments for user sitemap queries.
  • wp_sitemaps_users_pre_url_list – Filters the users URL list before it is generated (short-circuit).
  • wp_sitemaps_users_pre_max_num_pages – Filters the max number of pages before it is generated (short-circuit).

Stylesheets:

  • wp_sitemaps_stylesheet_css – Filters the CSS for the sitemap stylesheet.
  • wp_sitemaps_stylesheet_url – Filters the URL for the sitemap stylesheet.
  • wp_sitemaps_stylesheet_content – Filters the content of the sitemap stylesheet.
  • wp_sitemaps_stylesheet_index_url – Filters the URL for the sitemap index stylesheet.
  • wp_sitemaps_stylesheet_index_content – Filters the content of the sitemap index stylesheet.

How to Remove Pages from Yoast Sitemap?

Now that you are familiar with the reasons to delete links/pages from your YoastSEO sitemap, it’s time to discuss how to remove links from Yoast sitemap.

To remove a link from Yoast sitemap you need to head over to your WordPress dashboard. From the pages and posts, open the post or page you want to remove. Now from the YoastSEO box go to the advanced tab and change the “Allow search engines to show this post in search results?” from yes to no and update the post or page.

Now your post or page won’t be displayed in search engines.

In some cases, the process of removing a link can be time-consuming and boring. The other way to bulk remove links is by adding a code to your function.php file.

add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', function () {
return array( 311, 322 );
} );

In the array brackets, you can enter your posts and pages ID to delete them from your sitemap.

A sitemap includes a lot of links related to your website. The more sorted they are the better the sitemap works for your website’s SEO. Therefore, it is necessary to make some changes to your sitemap to keep it neat and limit the number of links used in your sitemap.

To change sitemap limitations, from your YoastSEO settings go to search appearance and personalize the settings.

As you probably know, some post types are crucial and need to be indexed, and some are used on your WordPress website. Therefore, if you are using many post types in your website, you can deactivate this option in YoastSEO from the content type tab.

You can also make changes to the media section of your sitemap and present it better to Google and other search engines.

How to Remove Categories from Sitemap?

To remove categories from your sitemap you need to head over to taxonomies tab. Once you are in this tab, you can change the categories settings in your YoastSEO sitemap. Also, you can change your tags settings in the taxonomies tab as well.

Some taxonomies may not be available in this section, therefore, you may need to use code to filter them out in your sitemap.

Example code to remove single taxonomy:

/* Exclude One Taxonomy From Yoast SEO Sitemap */
function sitemap_exclude_taxonomy( $value, $taxonomy ) {
if ( $taxonomy == 'taxonomy_slug' ) return true;
}
add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );

Example code to remove multiple taxonomies:

/* Exclude Multiple Taxonomies From Yoast SEO Sitemap */
add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );
function sitemap_exclude_taxonomy( $value, $taxonomy ) {
$taxonomy_to_exclude = array('taxonomy_slug1','taxonomy_slug2', 'taxonomy_slug3');
if( in_array( $taxonomy, $taxonomy_to_exclude ) ) return true;
}

Conclusion

Sitemap plays an important role in your website’s SEO. Google uses sitemap to index your website. Therefore, it is necessary to have clean and sorted sitemap.

In this article, we discussed many ways to filter sitemap. Keep in mind, to find a real reason to remove links from Yoast sitemap and then decide whether you should remove it or keep it. Because removing an important page/link can damage your sitemap and your website’s SEO.

LEAVE A REPLY

Please enter your comment!
Please enter your name here