Remove all products of specific category from sitemap

#792992
  • Resolved Shubham
    Rank Math free

    I want to remove all the products having the category “uncategorized”. Please help

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello,

    Thank you for reaching out to us. To remove all products of a specific category, such as “uncategorized,” from the sitemap using Rank Math, you can add a custom code snippet to your WordPress theme’s functions.php file or rank-math.php file.

    Here is the code snippet you can use:

    /**
     * Filter to remove all products of a specific category from the sitemap
     */
    add_filter('rank_math/sitemap/product/remove', function ($remove, $product) {
        if (has_term('uncategorized', 'product_cat', $product)) {
            return true;
        }
        return $remove;
    }, 10, 2);

    Replace ‘uncategorized’ with the slug of the category you want to exclude from the sitemap. Once you’ve added this code snippet, it will remove all products from the “uncategorized” category from the sitemap.

    If you have any further questions or need assistance, please feel free to ask. We are here to help!

    If you need more personalized assistance, please reply to this ticket, and a human agent will assist you promptly.

    Sources:
    Remove All Schema from Product Category Pages » Rank Math
    How to Disable Custom Product Redirection in Rank Math » Rank Math
    Rank Math General Settings

    Shubham
    Rank Math free

    This code is not working. Still all the products are there in sitemap

    Jokben
    Rank Math free

    It seems like the code you provided isn’t effectively removing all the products from the sitemap.

    Hello,

    To remove all your products under the uncategorized category, you may need to use this filter on your site:

    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
    
        if ( $type === 'post' && $object->post_type === 'product' ) //change your post type here
        {
    
            $categories = get_the_terms( $object->ID, 'product_cat' );
    
            // Replace 'category-slug-1' with your actual category slugs
            $exclude_categories = array( 'uncategorized');
    
            if ( is_array( $categories ) && array_intersect( $exclude_categories, wp_list_pluck( $categories, 'slug' ) ) ) {
                return false;
            }
        }
        return $url;
    }, 10, 3 );

    Here’s how you can add filter/hook to your WordPress site:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Once done, please flush your sitemap cache by following these steps:

    1. Flush the Sitemap cache by following this video screencast:
    https://i.rankmath.com/pipRDp

    2. Exclude the Sitemap files of the Rank Math plugin in your caching plugin. The cache could be via a plugin or from the server. For plugins or Cloudflare, please follow this article:
    https://rankmath.com/kb/exclude-sitemaps-from-caching/

    Hope that helps.

    Jokben
    Rank Math free

    this coading is not working please help me out

    Hello,

    Please try to replace the filter with the following one and see if that works for you:

    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
    
        if ( $type === 'post' ) //change your post type here
        {
    
            $categories = get_the_terms( $object->ID, 'category' );
            $exclude_categories = array( 'uncategorized');
    
            if ( is_array( $categories ) && array_intersect( $exclude_categories, wp_list_pluck( $categories, 'slug' ) ) ) {
                return false;
            }
        }
        return $url;
    }, 10, 3 );
    

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Shubham
    Rank Math free

    Thank you so much,
    The code is perfectly running

    Hello,

    We are super happy that this resolved your issue. 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.

Viewing 8 replies - 1 through 8 (of 8 total)

The ticket ‘Remove all products of specific category from sitemap’ is closed to new replies.