Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hayatbiralem/5fce973a95777e1993aa2399cc2ea090 to your computer and use it in GitHub Desktop.
Save hayatbiralem/5fce973a95777e1993aa2399cc2ea090 to your computer and use it in GitHub Desktop.
Yoast WP Seo - How to remove noindex category posts from sitemap
<?php
if (!class_exists('myprefix_remove_noindex_category_posts_from_sitemap')) {
class myprefix_remove_noindex_category_posts_from_sitemap
{
function __construct()
{
add_action('wpseo_exclude_from_sitemap_by_post_ids', [$this, 'exclude']);
}
function exclude()
{
$excluded_post_ids = [];
$no_index_categories = [];
$wpseo_taxonomy_meta = get_option('wpseo_taxonomy_meta');
if (isset($wpseo_taxonomy_meta['category']) && !empty($wpseo_taxonomy_meta['category'])) {
$no_index_categories = array_filter($wpseo_taxonomy_meta['category'], function ($meta) {
return isset($meta['wpseo_noindex']) && $meta['wpseo_noindex'] == 'noindex' ? true : false;
});
}
if (!empty($no_index_categories)) {
$no_index_category_ids = array_keys($no_index_categories);
$excluded_post_ids = get_posts([
'posts_per_page' => -1,
'category' => implode(',', $no_index_category_ids),
'fields' => 'ids',
]);
}
return $excluded_post_ids;
}
}
new myprefix_remove_noindex_category_posts_from_sitemap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment