HOMEBlogTechnical SEOExclude Post Type Sitemap: 3 Easy Code Methods…

Exclude Post Type Sitemap: 3 Easy Code Methods for Yoast SEO & Rank Math

Exclude Post Type Sitemap

A custom post type that should stay out of search results is still appearing in your XML sitemap. Use the correct PHP filter for Yoast SEO or Rank Math to Exclude Post Type Sitemap entries for that specific type, while leaving other sitemap content unchanged. The key is to target the registered post type slug, not the dashboard label.

Choose only the code snippet for the SEO plugin active on your site; the two plugins use different filters. Removing a post type from a sitemap only changes sitemap output—it does not add a noindex directive, block crawling, or remove pages that search engines have already indexed. Before deploying, identify the slug, place the code somewhere maintainable, and verify the generated sitemap after saving.

  • A WordPress site with Yoast SEO or Rank Math active and XML sitemaps enabled.
  • PHP 8.0 or the PHP version supported by your hosting environment.
  • The exact registered post type slug, such as book or event, rather than its display label.
  • A recent site backup and access to staging, or another safe way to test the change before production.
  • Access to a child theme, a small site-specific plugin, or a controlled snippets tool. Avoid editing a parent theme, since theme updates can overwrite your changes.

Prepare the Post Type and Choose a Safe Code Location

Before adding a sitemap filter, identify the post type’s registered slug. The slug is the machine-readable name used in code; it may differ from the label shown in the WordPress dashboard. For example, a dashboard menu labeled “Books” might use the slug book. Filters compare against that slug, so using a plural label or a display name can make a valid callback appear to do nothing.

Check the code that registers the custom post type, or ask the developer or plugin that created it. You can also inspect registered post types with WordPress functions. The official get_post_types reference describes how to retrieve registered types and their properties. Confirm the exact slug before proceeding, including its capitalization if the registration uses a nonstandard value.

Choose a location that will remain available when the site theme changes. A small site-specific plugin or a must-use plugin is a strong choice for functionality that belongs to the site rather than its presentation. A child theme’s functions.php is also suitable when the behavior is intentionally tied to that theme. A snippets tool can work for a short filter if it runs on the front end and is managed by someone with appropriate access. Do not add the code to a parent theme, where an update could erase it.

Use the snippet for only the SEO plugin active on the site, and test the change on staging when possible. Keep a copy of the original code and note where the new filter is installed so another maintainer can find it later.

Backup Required
Back up the site and test the change before deploying it to production. A recent database and file backup gives you a recovery path if a coding mistake interrupts the site.

Exclude the Post Type from the Yoast SEO Sitemap

Use this filter only on a site running Yoast SEO. It is not a substitute for Rank Math’s separate sitemap filter. Yoast applies wpseo_sitemap_exclude_post_type with the current exclusion value and the post type slug, allowing your callback to exclude one exact type without changing the others.

Add this snippet to a site-specific plugin, a child theme’s functions.php, or a snippets tool that runs PHP. Replace event with the registered slug you confirmed earlier. The callback returns true for that slug and passes the existing exclusion value through for every other post type.

PHP
function pnet_yoast_exclude_event_post_type( $excluded, $post_type ) {
    if ( 'event' === $post_type ) {
        return true;
    }

    return $excluded;
}
add_filter( 'wpseo_sitemap_exclude_post_type', 'pnet_yoast_exclude_event_post_type', 10, 2 );

The snippet does not include an opening <?php tag. Paste it without one in a snippets tool or inside an already-open PHP file, such as a plugin or functions.php. If you are creating a new PHP file, include the opening tag at the top of that file, outside the shortcode.

Save the change, then open your Yoast sitemap index, commonly available at https://example.com/sitemap_index.xml, replacing the domain with your own. Confirm the sitemap for the excluded type is absent and that expected entries for other post types remain. If the old entry still appears, clear page, plugin, server, and CDN caches before testing again.

Exclude Post Type Sitemap - Yoast Index Check
Yoast Index Check

Exclude the Post Type from the Rank Math Sitemap

Use this implementation on sites running Rank Math. Its rank_math/sitemap/exclude_post_type filter is separate from Yoast SEO’s filter, so do not use the Yoast hook as a replacement. Rank Math passes the current exclusion state and the post type slug to the callback. Return true for the type you want to omit, and preserve the incoming state for all other types.

In this example, event is the registered post type slug. Replace it with the exact slug from your site, not the dashboard label. The callback name is distinct from the Yoast example, which avoids a function redeclaration error if both examples are kept in the same codebase.

PHP
function pnet_rank_math_exclude_event_post_type( $excluded, $post_type ) {
    if ( 'event' === $post_type ) {
        return true;
    }

    return $excluded;
}
add_filter( 'rank_math/sitemap/exclude_post_type', 'pnet_rank_math_exclude_event_post_type', 10, 2 );

The snippet has no opening <?php tag. Paste it without one into a snippets tool or into a PHP file that is already open, such as a site-specific plugin or child theme functions.php. Add an opening tag only when creating a new PHP file, and keep it outside the shortcode.

After saving, visit the Rank Math sitemap index, commonly found at https://example.com/sitemap_index.xml, using your own domain. Confirm that the sitemap for the targeted post type is absent while the sitemap links for other expected types remain. This checks that the filter is scoped correctly rather than suppressing sitemap output more broadly. If the index still shows the old entry, clear page, plugin, server, and CDN caches, then reload the index in a private browser window.

Exclude Post Type Sitemap - Rank Math Index Check
Rank Math Index Check

Adapt the Filter for Multiple Post Types and Safe Deployment

To exclude more than one registered post type, compare the incoming slug against an array of exact slugs with strict matching. Keep the callback and hook specific to the SEO plugin you use: the examples below are alternatives, so install only the matching callback and filter registration. Replace event and book with your site’s registered slugs.

For Yoast SEO, use this version. It returns true only when the current post type matches one of the listed slugs; otherwise, it returns the exclusion value Yoast supplied.

PHP
function pnet_yoast_exclude_selected_post_types( $excluded, $post_type ) {
    $excluded_post_types = array( 'event', 'book' );

    if ( in_array( $post_type, $excluded_post_types, true ) ) {
        return true;
    }

    return $excluded;
}
add_filter( 'wpseo_sitemap_exclude_post_type', 'pnet_yoast_exclude_selected_post_types', 10, 2 );

For Rank Math, use this alternative instead. Its callback follows the same matching logic, but attaches to Rank Math’s own filter. The distinct pnet_-prefixed function names also help prevent redeclaration errors if you retain both versions in a codebase; do not register both filters unless both plugins are intentionally active and you specifically need both integrations.

PHP
function pnet_rank_math_exclude_selected_post_types( $excluded, $post_type ) {
    $excluded_post_types = array( 'event', 'book' );

    if ( in_array( $post_type, $excluded_post_types, true ) ) {
        return true;
    }

    return $excluded;
}
add_filter( 'rank_math/sitemap/exclude_post_type', 'pnet_rank_math_exclude_selected_post_types', 10, 2 );

Do not match display labels, singular names, or URL fragments: those can differ from the registered slug and may change independently. Preserving $excluded for nonmatching types is important because it respects the SEO plugin’s existing decision instead of overriding it.

Pro Tip
Test the selected plugin version on staging first, then keep the approved snippet in version control or a site-specific plugin so it is easy to review and maintain.

Verify the Sitemap and Understand What Exclusion Changes

After saving the filter, open the sitemap index generated by your active SEO plugin. Confirm the sitemap for the targeted post type is absent, then check that expected entries for posts, pages, and any other included types still appear. If the plugin exposes a sitemap directly for that type, try its previous URL as well; the expected result is that it no longer lists the targeted content. A missing index entry is the clearest confirmation that the post type has been excluded from sitemap output.

If the old entry or URLs remain visible, clear caches in a deliberate order: page-cache plugin, host or server cache, and CDN cache. Then reload the sitemap in a private browser window to reduce the chance of seeing a locally cached response. Also verify that the intended SEO plugin is active and XML sitemaps are enabled. Confirm that the snippet is installed in the environment you are testing, runs on the front end, and has no syntax error or disabled status in your snippets tool.

When the sitemap remains unchanged after those checks, recheck the registered post type slug and ensure the filter matches the active plugin. A sitemap can be cached, but an incorrect hook, inactive snippet, or wrong slug means the callback may not be executing at all. Test on staging when available before changing production code.

Most importantly, sitemap exclusion is not an indexing directive. It does not add a noindex meta tag, block crawling in robots.txt, or remove URLs that search engines have already indexed. Search engines can still discover URLs through internal links or external links. If you also need pages kept out of search results, choose and implement an appropriate indexing policy separately, then verify that policy independently.

You might also like:

Rank Math Custom Variables: 5 Easy Steps to Add Dynamic Meta Titles in WordPress

Rank Math Custom Variables missing from meta titles? Add them in WordPress with our step-by-step guide.

Read more →

Common Errors

When the post type still appears or the site reports a PHP error after adding a filter, check the implementation systematically. A sitemap index may also be cached, so do not assume that a visible old entry means the callback failed.

The Post Type Still Appears in the Sitemap

Check these items in order. First, confirm that the value in the callback exactly matches the registered post type slug, not its dashboard label. Next, confirm which SEO plugin is active and use only its matching filter: Yoast SEO uses wpseo_sitemap_exclude_post_type, while Rank Math uses rank_math/sitemap/exclude_post_type. A filter for the other plugin will not affect the sitemap being generated.

Then confirm that the callback accepts both arguments and that add_filter() is registered with an accepted-arguments value of 2. Check that the snippet is enabled and loads on the front end, and that it is installed in the site environment whose sitemap you are testing. Finally, clear the page-cache plugin, host or server cache, and CDN cache, then reload the sitemap in a private browser window. Verify that unrelated sitemap entries remain present.

A PHP Fatal or Redeclaration Error Occurs

Look for a callback function defined more than once, especially if you copied both plugin examples into the same file or enabled a duplicate snippet. Rename the function with a unique pnet_ prefix or remove the duplicate definition. Also check for missing braces, semicolons, or other syntax mistakes. Some snippets tools expect PHP code without an opening <?php tag; an extra tag can trigger an error.

If the error prevents normal dashboard access, use your host’s file manager, SFTP, or the snippets tool’s recovery mode to disable or remove the faulty snippet. Restore the backed-up version if necessary, then correct and test the code on staging before re-enabling it. Remember that removing a sitemap entry does not add noindex or remove already indexed URLs; handle indexing separately when required.

You might also like:

How to Easily Fix Broken Links in WordPress After a Site Migration

Struggling with missing images? Learn how to fix broken links in WordPress after a site migration instantly using plugins, SQL,...

Read more →

Conclusion

Yoast SEO and Rank Math each provide a dedicated sitemap filter for excluding a registered post type. Use the filter that matches the active plugin, compare against the exact post type slug, and return the existing exclusion value for every other type. This keeps the change focused instead of unintentionally altering sitemap output for unrelated content.

Before deployment, confirm the slug and save a current backup. Add the callback in a maintainable location, such as a site-specific plugin or child theme, and test it on staging when possible. After saving, clear relevant page, server, plugin, and CDN caches, then inspect the sitemap index to verify that the intended post type is absent and other expected sitemap entries remain. If you copied both provider examples, ensure they have distinct function names and enable only the integration you need.

Finally, treat sitemap inclusion and search indexing as separate concerns. Excluding a post type from the sitemap does not add a noindex directive, prevent crawling, or remove URLs already indexed by search engines. Set and verify an indexing policy separately if the content should not appear in search results. With the correct plugin filter, slug, and post-deployment checks, you can reliably Exclude Post Type Sitemap entries without changing the rest of your sitemap.

Abhik

🚀 Full Stack WP Dev | ☕ Coffee Enthusiast | 🏍️ Biker | 📈 Trader
Hi, I’m Abhik. I’ve been coding since 2007, a journey that began when I outgrew Blogger and migrated to a robust self-hosted stack. That transition introduced me to WordPress, and I’ve been building professional solutions ever since.

Leave a comment