Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Last active February 17, 2020 14:36
Show Gist options
  • Save ronalfy/031ab5aeb00c025e7049e363ca8c2ca4 to your computer and use it in GitHub Desktop.
Save ronalfy/031ab5aeb00c025e7049e363ca8c2ca4 to your computer and use it in GitHub Desktop.
Exclude Paid Membership Pro My Account page and child pages from Yoast XML Sitemap
<?php
add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', 'pmpro_my_account_exclude', 10, 1 );
/**
* Exclude page 'my-account' and child pages.
*
* @param array $excluded_ids The excluded post ids.
* @return array Updated exlucded IDs.
*/
function pmpro_my_account_exclude( $excluded_ids = array() ) {
$maybe_page_ids = wp_cache_get( 'pmpro_exclude_pages' );
if ( $maybe_page_ids ) {
return array_merge( $maybe_page_ids, $excluded_ids );
}
$page_ids = array();
$page = get_page_by_path( 'my-account' );
if ( $page ) {
$page_ids[] = $page->ID;
$posts = get_posts(
array(
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $page->ID,
'posts_per_page' => 100, // modify this to account for the number of members.
)
);
if ( $posts ) {
foreach ( $posts as $post ) {
$page_ids[] = $post->ID;
$child_args = array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 100,
'post_parent' => $post->ID,
);
$child_posts = get_posts( $child_args );
if ( $child_posts ) {
foreach ( $child_posts as $child_post ) {
$page_ids[] = $child_post->ID;
}
}
}
}
}
wp_cache_set( 'pmpro_exclude_pages', $page_ids );
return array_merge( $page_ids, $excluded_ids );
}
@ronalfy
Copy link
Author

ronalfy commented Feb 17, 2020

Tweak https://gist.github.com/ronalfy/031ab5aeb00c025e7049e363ca8c2ca4#file-pmpro-exclude-pages-from-sitemap-php-L23 for how many members to process. Will be slower the more members are excluded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment