Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I programmatically exclude post types from the Yoast sitemap


How do I programmatically exclude post types from the Yoast sitemap


To programmatically exclude post types from the Yoast sitemap, you can use the `wpseo_sitemap_exclude_post_type` filter. Here is an example of how to do it:

php
add_filter( 'wpseo_sitemap_exclude_post_type', 'exclude_post_types', 10, 2 );

function exclude_post_types( $value, $post_type ) {
    $post_types_to_exclude = array( 'post_type1', 'post_type2', 'post_type3' );
    if ( in_array( $post_type, $post_types_to_exclude ) ) {
        return true;
    }
    return $value;
}

In this example, the `exclude_post_types` function checks if the current post type is in the array of post types to exclude. If it is, it returns `true`, which tells Yoast SEO to exclude the post type from the sitemap. If it is not, it returns the default value of `$value`, which is `false`.

You can add this code to your theme's `functions.php` file or create a custom plugin to include this code.

This method allows you to programmatically exclude specific post types from the Yoast sitemap without having to manually enter the post type IDs in the Yoast SEO settings.

Citations:
[1] https://www.binarycarpenter.com/how-to-exclude-custom-post-types-posts-pages-from-yoast-seo-sitemap/
[2] https://wordpress.stackexchange.com/questions/243476/exclude-custom-post-type-in-sitemap-generation
[3] https://stackoverflow.com/questions/44530358/wordpress-yoast-exclude-posts-from-xml-sitemap
[4] https://clicknathan.com/web-design/ocho_no_individual_testimonials_in_sitemap/
[5] https://yoast.com/help/sitemap-shows-excluded-posts-pages/