25% off ProSNIPC25OFF

Exclude Specific Posts or Pages from the XML Sitemap

Removes a configurable list of post IDs from the WordPress core XML sitemap to prevent thin content from being indexed.

PHPby SnipCraft
php
<?php
if ( ! function_exists( 'scseed_exclude_from_sitemap' ) ) {
    // Add the IDs of any posts, pages, or custom-post-type items to exclude.
    function scseed_exclude_from_sitemap( $args ) {
        $excluded_ids = [ 10, 25, 42 ]; // replace with your actual post/page IDs

        if ( ! isset( $args['post__not_in'] ) || ! is_array( $args['post__not_in'] ) ) {
            $args['post__not_in'] = [];
        }
        $args['post__not_in'] = array_unique(
            array_merge( $args['post__not_in'], array_map( 'absint', $excluded_ids ) )
        );
        return $args;
    }
    add_filter( 'wp_sitemaps_posts_query_args', 'scseed_exclude_from_sitemap' );
}
#crawling#indexing#seo#sitemap#xml-sitemap