1

I want to eliminate some links and articles from the sitemap page. I have tried using a plugin called "WordPress Simple HTML Sitemap" and even tried from within WordPress itself but haven't been successful.

Is there a free, effective plugin or another method to do this?

My last resort is to apply CSS {display: none} to those pages using the inspector, but I want a better solution.

I have tried using the third party plugins.

2 Answers 2

3

To do it without a plugin, use the wp_sitemaps_posts_query_args hook, setting the post__not_in argument. Add this code to functions.php:

add_filter('wp_sitemaps_posts_query_args', function($args, $post_type) {

  // for post types other than pages, exit without doing any customisation
  if ('page' !== $post_type) return $args;

  // set the page IDs to exclude
  $args['post__not_in'] = array(111, 222, 333);
  return $args;

}, 10, 2);
Sign up to request clarification or add additional context in comments.

Comments

1

WordPress actually provides a few different ways you can modify the sitemap.

The first is using the wp_sitemaps_posts_query_args filter which allows you to modify the query WordPress uses to get posts for the sitemap.

If you know the IDs of the posts/pages, you can modify the query like Brett mentioned in another comment:

add_filter('wp_sitemaps_posts_query_args', function($args, $post_type) {

   // set the page IDs to exclude
  $args['post__not_in'] = array(111, 222, 333);

  return $args;

}, 10, 2);

If you don't know the IDs of the posts/pages, but you know the permalinks, there is another way you can modify the sitemap: the wp_sitemaps_posts_entry filter. This filter allows you to modify the sitemap entry for an individual post. See: https://github.com/WordPress/wordpress-develop/blob/6.8.2/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php#L164-L164

WordPress passes the entry through a filter like so:

$sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type );

Where $sitemap_entry is an array of all the fields that make up the entry for the sitemap. If you know the url of the post/page you'd like to exclude you can filter it like so:

add_filter('wp_sitemaps_posts_entry', function($sitemap_entry, $post, $post_type) {
    
    // if it's a page post type
    if( 'page' === $post_type) {
        if ( isset( $sitemap_entry['loc'] ) && $sitemap_entry['loc'] === "https://yourdomain.com/the-url-of-the-page-you-want-to-exlude" ) {
            $sitemap_entry = [];
        }
    }
    
    return $sitemap_entry;

}, 10, 2);

By the setting the entry to an empty array ([]) WordPress won't include it in the sitemap.

References

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.