[WP Plugin] Yoast SEO が生成する sitemap.xml から特定のポストやページを除外する

functions.php から用意されているフィルターを利用してまとめて除外します。

> – [How to exclude content from the sitemap – Yoast Knowledge Base](https://kb.yoast.com/kb/sitemap-shows-excluded-posts-pages/)

除外したいページのスラッグを配列で用意し、ページ ID に変換してから `get_page_by_path()` に渡します。

“`php:functions.php
/**
* Exclude post or pages from the Yoast SEO sitemap.
*
* @return array
*/
function sitemap_exclude_pages() {
$page_slugs = [ ‘exclud_slug_1’, ‘exclud_slug_2’ ];
$page_ids = [];

foreach ( $page_slugs as $value ) {
$obj = get_page_by_path( $value );

if ( null !== $obj ) {
$page_ids[] = $obj->ID;
}
}

return $page_ids;
}
add_filter( ‘wpseo_exclude_from_sitemap_by_post_ids’, ‘sitemap_exclude_pages’ );
“`