# Exclude Specific Posts and Pages From the Sitemap

> Leaves the posts and pages you list out of the built-in XML sitemap, such as thank-you pages, landing pages for ads or legal pages.

- Type: PHP
- Runs: Run everywhere
- Category: [SEO](https://wpide.com/library/category/seo/)
- Tags: sitemap, xml, exclude
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/exclude-posts-from-sitemap/

## Code

```php
// IDs of posts, pages or custom post type entries to leave out.
$wpide_sitemap_excluded_ids = array( 0 );

add_filter( 'wp_sitemaps_posts_query_args', function ( $args ) use ( $wpide_sitemap_excluded_ids ) {
    $args['post__not_in'] = array_merge( isset( $args['post__not_in'] ) ? (array) $args['post__not_in'] : array(), array_map( 'absint', $wpide_sitemap_excluded_ids ) );

    return $args;
} );
```

## Notes

Add the post or page IDs to the list. This does not noindex the pages; combine it with a noindex rule if they should not appear in search at all.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Exclude Specific Posts and Pages From the Sitemap" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
