skip to Main Content

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


  1. 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);
    
    Login or Signup to reply.
  2. There are plugins like Yoast and Rankmath which allow you to remove specific pages or post from sitemap, but still you want a code then please use this.

    Here’s how you can exclude specific pages from the default WordPress sitemap:

    Add Code

    1. Open Your Theme’s functions.php file.

    2. Add the Following Code:

      add_filter('wp_sitemaps_posts_query_args', 'exclude_pages_from_sitemap', 10, 2);
      
      function exclude_pages_from_sitemap($args, $post_type) {
          // List of page IDs to exclude from the sitemap
          $excluded_ids = array(123, 456); // Replace with your page IDs
      
          // Check if we are modifying the query for pages
          if ($post_type === 'page') {
              if (!isset($args['post__not_in'])) {
                  $args['post__not_in'] = array();
              }
      
              // Merge the excluded IDs with any existing excluded IDs
              $args['post__not_in'] = array_merge($args['post__not_in'], $excluded_ids);
          }
      
          return $args;
      }
      

    I am using this on my website, so I share it for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search