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.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.