How to NoIndex All Posts in a Product Category

#290232
  • Resolved Nicolas Saorin Boti
    Rank Math free

    I read your artible “How to NoIndex All Posts in a Category”.

    I would like to do the same, with a Snippet, but for the products under one Product Category.

    Thank you

Viewing 1 replies (of 1 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    You can easily do that from the product list with Rank Math PRO. Here’s a link for more information:
    https://rankmath.com/kb/bulk-editing-in-rank-math/#num-3-3-set-to-noindex-pro

    Here’s a screencast for your reference using Rank Math PRO:
    https://i.rankmath.com/n2mnan

    However, here’s a filter you can try to achieve that on your products under a category:

    /**
     * Filter to set noindex to specific categories
     */
    add_filter( 'rank_math/frontend/robots', function ( $robots ) {
    $slugs = array();
    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
    foreach ($product_cats as $product_cat){
        $slugs[] = $product_cat->slug;
    }
    
    if ( in_array( 'category2', $slugs ) ) {
                    unset( $robots['index'] );
                    $robots['noindex'] = 'noindex';
            }
            return $robots;
    } );

    Update the category2 to your actual product category slug.

    I hope that helps.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 1 replies (of 1 total)

The ticket ‘How to NoIndex All Posts in a Product Category’ is closed to new replies.