How to exclude pages or posts from sitemap in WordPress?

H

This post contains affiliate links. If you click through and make a purchase, I may receive a commission (at no additional cost to you). This helps support the blog and allows me to continue to make free content. Thank you for your support!


 

wordpress logoA blogger or webmaster may add the robots meta noindex or nofollow tag to irrelevant or thin pages or posts. When you add the attribute to pages, search engines will remove the pages from their index. If you don’t remove the no-indexed posts from the sitemap, search engines might crawl the posts even though they’re not relevant to your site. If you want Googlebot or Bingbot to crawl only the important pages on your site, follow the below two steps.

How to exclude a page from sitemap in WordPress?

Although some WordPress SEO plugins allow you to add exclude pages from sitemap by their ID, this option is of no use if you want to exclude 100s of posts. Likewise, WordPress lets you make posts private. This option is not useful if you want the post to be searchable.

Here’s a trick you can use to remove several pages from the sitemap generated by the most popular SEO plugin i.e. Yoast:

Step 1: Bulk add tag to a post

Note down the posts you don’t want to see the sitemap and group them by adding a new tag. If the posts are in sequence, select the post with the bulk selector utility. Hover the mouse cursor on the page/post title and select the quick edit option. When you do so, WP will display options to add a tag, change category, etc. Enter your desired tag name and click on the Submit/Ok button.

If the post has some words in common and if they are not in sequence, use the Automattic Post Tagger plugin to add tags to posts.

Step 2: Edit functions.php file

Note down the tag and open the functions.php file of the child theme.

Open the text editor and copy/paste the following lines into a new file.

function sitemap_exclude_taxonomy( $value, $taxonomy ) {
if ( $taxonomy == 'xxx' ) return true;
}
add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );

Replace xxx with the tag name. Once you do so, copy the code and paste it at the end of the functions.php file. Don’t worry, the code won’t break or crash your site. It has been provided by Yoast. I’m using the same code on on my sites. It has helped me remove 400+ posts rom the sitemap.

Save the file. That’s it!

About the author

Pramod

My name is Pramod. I’m a software developer (with good knowledge on PHP, JAVA – Android, JavaScript, CSS), blogger and online marketing professional. I’ve 3 years of experience as a blogger and 2 years of full time experience as a software developer.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me