The Slim SEO plugin adds every public post type and taxonomy of your WordPress site to sitemap.xml. Helper post types, such as sliders, popups or help entries, then end up in the sitemap even though their pages are not meant to be found.
This snippet excludes the post types and taxonomies you list from the Slim SEO sitemap. The example removes the help and slider post types and the category taxonomy.
The snippet
<?php
/**
* Exclude post types and taxonomies from the Slim SEO sitemap.
*/
function dpc_slim_seo_sitemap_exclusions() {
// ===== Config =====
return array(
'post_types' => array( 'help', 'slider' ), // Post type slugs.
'taxonomies' => array( 'category' ), // Taxonomy slugs.
);
// ===== End of config =====
}
add_filter( 'slim_seo_sitemap_post_types', 'dpc_slim_seo_sitemap_post_types' );
function dpc_slim_seo_sitemap_post_types( $post_types ) {
$exclude = dpc_slim_seo_sitemap_exclusions()['post_types'];
return array_values( array_diff( (array) $post_types, $exclude ) );
}
add_filter( 'slim_seo_sitemap_taxonomies', 'dpc_slim_seo_sitemap_taxonomies' );
function dpc_slim_seo_sitemap_taxonomies( $taxonomies ) {
$exclude = dpc_slim_seo_sitemap_exclusions()['taxonomies'];
return array_values( array_diff( (array) $taxonomies, $exclude ) );
}
How it works
- Slim SEO builds its list of sitemap post types and taxonomies on
initand passes them throughslim_seo_sitemap_post_typesandslim_seo_sitemap_taxonomies. - Each callback removes the slugs from your config with
array_diff(). Slugs that do not exist on your site are ignored, so nothing breaks if you remove a post type later. - The excluded types get no
sitemap-post-type-….xmland disappear from the sitemap index. Their URLs return Slim SEO’s “Invalid sitemap URL” message. - The filters only change the sitemap. The pages stay public and indexable.
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty. Slim SEO reads the filters on init, after Scripts Organizer has loaded the code.
The same code also works unchanged in a small plugin or an mu-plugin.
Slim SEO caches nothing here, but your browser or a page cache may still show the old sitemap.xml. Open it in a private window to check.
Customise it
- Edit the two arrays in the config. Find a post type or taxonomy slug in the admin URL:
edit.php?post_type=helporedit-tags.php?taxonomy=category. - Exclude single posts by ID instead of whole post types with Slim SEO’s
slim_seo_sitemap_post_ignorefilter:
add_filter( 'slim_seo_sitemap_post_ignore', function ( $ignore, $post ) {
return in_array( $post->ID, array( 12, 34 ), true ) ? true : $ignore;
}, 10, 2 );
Gotchas
- Leaving a page out of the sitemap does not keep it out of Google. If a post type should not be indexed at all, set it to noindex in Slim SEO’s settings, which also removes it from the sitemap.
- The snippet does nothing without Slim SEO. WordPress’ own
wp-sitemap.xmluses other filters (wp_sitemaps_post_types,wp_sitemaps_taxonomies), and Slim SEO switches it off.
In the Scripts Organizer library Import it with one click from Scripts Organizer.