Hello,
We apologize for the issue you are facing with excluding the product category from the sitemap. The code you provided seems to be on the right track, but there might be a small issue with how the filter is applied or how the conditions are checked.
Here’s a refined version of your code that should work:
function exclude_product_category_from_sitemap( $url, $type, $object ) {
// Check if the object is a product category
if ( 'term' === $type && 'product_cat' === $object->taxonomy ) {
$excluded_category = 2136; // ID of the category to exclude
// Check if the current term is the excluded category or a child of it
if ( $object->term_id == $excluded_category || term_is_ancestor_of( $excluded_category, $object->term_id, 'product_cat' ) ) {
return false; // Exclude from sitemap
}
}
return $url;
}
add_filter( 'rank_math/sitemap/entry', 'exclude_product_category_from_sitemap', 10, 3 );
Make sure to replace the existing code with this one in your theme’s rank-math.php file. After making these changes, clear your sitemap cache and regenerate the sitemap to see if the category and its subcategories are excluded as expected.
If you continue to experience issues, please reply to this ticket, and a human agent from the Rank Math Support team will assist you further. Thank you for raising this concern!
Sources:
How to Make Your Theme Rank Math Compatible » Rank Math
The code provided by RankBot is not working.
Thank you
After cache cleared, the code work. Thank you
Hello,
We are super happy that the code now works on your end. If you have any other questions in the future, know that we are here to help you.
If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.
If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
Thank you.