Skip to content

Filter Jetpack’s XML Sitemaps

One thing I really liked—past tense, as I recently stopped using Yoast on this very website—about Yoast SEO’s XML sitemaps is the ability to exclude specific posts and pages. I don’t necessarily want a ‘Thank You’ page popping up in search results, you see.

Turns out Jetpack allows for that, too—in code, which I actually like! Imagine a theme that allows for all of a page’s child pages to be embedded in a grid layout of sorts. There’d be no need to ever point to the individual child pages. You can now simply exclude all of them—including possible future child pages, like so:

function my_sitemap_skip_post( $skip, $post ) {
  if ( 2146 === $post->post_parent ) { // '2146' would be the page of which the children are to remain 'hidden'.
    $skip = true;
  }

  return $skip;
}
add_filter( 'jetpack_sitemap_skip_post', 'my_sitemap_skip_post', 10, 2 );