Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save loorlab/442effb115e5e8ae4da481da15f0e368 to your computer and use it in GitHub Desktop.
Save loorlab/442effb115e5e8ae4da481da15f0e368 to your computer and use it in GitHub Desktop.
Exclude Sitemap YOAST + noindex - nofollow by ID - WP
<?php
/* YOAST SEO*/
add_filter('wpseo_exclude_from_sitemap_by_post_ids', 'exclude_persons_cpt_from_sitemap');
function exclude_persons_cpt_from_sitemap($excluded_posts) {
// ID para excluir del sitemap
$specific_posts_to_exclude = array(1147);
// ID PERSON
$args = array(
'post_type' => 'person',
'posts_per_page' => -1,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'exclude_from_sitemap',
'value' => '1',
'compare' => '='
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) {
$excluded_posts = array_merge($excluded_posts, $query->posts);
}
$excluded_posts = array_merge($excluded_posts, $specific_posts_to_exclude);
wp_reset_postdata();
return $excluded_posts;
}
add_filter('wpseo_robots', 'yoast_seo_robots_change_for_persons', 10, 1);
function yoast_seo_robots_change_for_persons($robots) {
// ID
$specific_post_ids = array(1147);
// noindex - nofollow
if (is_singular('person') && in_array(get_the_ID(), $specific_post_ids)) {
return 'noindex, nofollow';
} else {
return $robots;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment