Exclude posts with tag
-
Is there a way to make the sitemap not include posts that have a particular taxonomy term applied to them?
-
Hello @nitrospectide,
Thank you for contacting Rank Math support.
You can modify and use the following filter to exclude the posts with a specific term from the sitemap:add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){ return $url;}, 10, 3 );If you want us to modify it, please share the taxonomy and term name.
Looking forward to helping you.taxonomy: success_types
term: testimonials
Hello @nitrospectide,
Please use the following filter to exclude the posts that have the “testimonial” term from the sitemap:add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
// Only run for posts
if ( 'post' === $type && $object instanceof WP_Post ) {
// Check if post has the term "testimonials" in taxonomy "success_types"
if ( has_term( 'testimonials', 'success_types', $object->ID ) ) {
return false; // Exclude from sitemap
}
}
return $url; // Keep others
}, 10, 3 );Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Thank you. I used your code with one minor tweak to reference the post type key, instead of just ‘post’
add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
// Only run for posts
if ( 'success_stories' === $type && $object instanceof WP_Post ) {
// Check if post has the term "testimonials" in taxonomy "success_types"
if ( has_term( 'testimonials', 'success_types', $object->ID ) ) {
return false; // Exclude from sitemap
}
}
return $url; // Keep others
}, 10, 3 );Then to regenerate the sitemap, it’s my understanding that I need to change the Links Per Sitemap setting and save it. But the posts I was trying to remove from my sitemap are still there.
Hello @nitrospectide,
Please share the sitemap URL so we can check and assist you further.
Looking forward to helping you.eos-gnss.com/success_stories-sitemap.xml
All success stories with ‘testimonials’ in the URL should be hidden, since they have the testimonials taxonomy term applied to them. Example:
/successses/testimonials/tommy-shifflett
-
This reply was modified 5 months, 2 weeks ago by
nitrospectide.
Also: I just noticed that there are 79 posts with this term applied, and it’s only 2 that are showing in the sitemap. The 2 have ‘Index’ checked in the Rank Math section of the post edit screen, while the others all appear to have ‘No Index’ checked.
My goal with this code is to override the Index/No Index setting so that admins could not let these slip through to the sitemap by forgetting to check ‘No Index.’
Hello @nitrospectide,
The code should work fine if the taxonomy and post type slug are correct. Please follow this screencast to clear the sitemap cache: https://i.rankmath.com/pipRDp
If the issue persists, please get in touch with our development team at [email protected] so they can check this further.
Looking forward to helping you. -
This reply was modified 5 months, 2 weeks ago by
You must be logged in to reply to this topic.