Adding ‘noindex’ to external WooCommerce products

#669772
  • Resolved Thomas Knol
    Rank Math free

    Hello,

    I’ve read the developer docs on filters and hooks. Unfortunately it doesn’t include what i’m trying to achieve. Can you help me with the needed code to give external WooCommerce products a noindex robots tag in the <head> of my website?

    Some background information: we import external/affiliate products to our store and would like to noindex those product pages. But only those external productpages because we also have simple and variable products that need to keep the current index status.

    Thanks in advance.

    Thomas

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

    We appreciate you reaching out to us with your concern about adding a noindex robots tag to external WooCommerce products on your website. To achieve this, you can use the following code snippet and add it to your theme’s functions.php file or a custom plugin:

    /**
     * Filter to set noindex to specific categories
     */
    add_filter('rank_math/frontend/robots', function ($robots) {
        if (is_product() && is_external()) {
            unset($robots['index']);
            $robots['noindex'] = 'noindex';
        }
        return $robots;
    });

    This code snippet will check if the current page is a product page and if it is an external product. If both conditions are met, it will add a noindex robots tag to the page.

    Please ensure to replace is_external() with the appropriate function that checks if a product is external in your WooCommerce setup.

    If you have any further questions or need additional assistance, feel free to let us know.

    Thank you for reaching out to us!

    Best regards.

    Sources:
    How to NoIndex All Posts in a Category » Rank Math
    How to Fix the “Indexed, though blocked by robots.txt” Error » Rank Math
    Why Google Hasn’t Indexed Your Post, Page, and/or Website? » Rank Math
    How to Use the Noydir Robots Meta Tag » Rank Math

    Above code suggested by the bot does not work, it crashes the website. I’ve found a way to add a new line of <meta name =”robots”… but i want to replace the current one. Can you help me set this up for external woocommerce products only?

    Hello,

    Thank you for your query, and we are so sorry about the trouble this must have caused.

    To set the meta robots as noindex for WooCommerce products that are of the External/Affiliate type, you will need to adjust the code to check the product type and then modify the $robots array accordingly.

    Here’s the code that will do it for you:

    /**
     * Filter to set noindex for specific WooCommerce External/Affiliate product types
     */
    add_filter( 'rank_math/frontend/robots', function ( $robots ) {
        if ( is_product() ) {
            global $product;
            if ( $product->is_type( 'external' ) ) {
                $robots['index']  = 'noindex';
                $robots['follow'] = 'nofollow';
            }
        }
        return $robots;
    });
    

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

    We hope that helps, and please don’t hesitate to get in touch if you have any other questions.

    Thank you.

    Hi Alhan,

    Thanks for your reply.

    Unfortunately this gives me a cricital error on external productpages (the well known standard wordpress critical error). What can i do to fix this? When i run the code without the if statements it does add the noindex and nofollow to alle my pages. But the if statement breaks the page. Looking forward to your reply.

    I’ve managed to get it working with the code below:

    /** Filter to set noindex for WooCommerce External/Affiliate products **/
    add_filter( ‘rank_math/frontend/robots’, function ( $robots ) {
    if ( is_product() ) {
    global $post;

    $product_id = $post->ID;
    $product = wc_get_product( $product_id );

    if ( $product && $product->is_type( ‘external’ ) ) {
    $robots[‘index’] = ‘noindex’;
    $robots[‘follow’] = ‘nofollow’;
    }
    }
    return $robots;
    });

    Now i would like to also exclude these external products from the XML sitemap. I’ve checked the docs but can’t find the code to do that. Can you help me how i can achieve this? Even though the external productpages now have a ‘noindex, nofollow’ directive, they are still in the XML sitemap.

    Hello,

    To filter the items out of the sitemap before the generation the following filter can be used: https://rankmath.com/kb/filters-hooks-api-developer/#filter-sitemap-item

    We have created a filter that might work, but we ask that you test this and make the necessary changes to cater to your website specific needs:

    
    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
    	if ( $type == 'product' ) {
    		$product_id = $object->ID;
    		$product = wc_get_product( $product_id );
    		
    		if ( $product && $product->is_type( 'external' ) ) {
    			$url = false;
    		}
    	}
    	return $url;
    }, 10, 3 );
    

    Hope this helps solve your issues.

    Don’t hesitate to get in touch if you have any other questions.

    Hi Miguel,

    Unfortunately the above code does nothing to my XML Sitemap. I’ve regenerated the XML Sitemap by saving changes again on the Sitemap settings page. My Sitemap still has all the External WooCommerce products in it (please check the URL added in the sensitive data section).

    You have any idea how i can get it to work?

    Thanks in advance.

    Hello,

    We’re unable to determine which ones are external in your product sitemaps. Could you please share some of the external product URLs so we can check them?

    Also, we might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.

    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Hi Reinelle,

    For anybody else except for me as a site owner it’s hard to determine which products are external and which are not. But here you find an example of a external product.

    In your code we need to check if it’s an external product by checking it like below:

    if ( $product && $product->is_type( ‘external’ ) )

    This works for setting the robots meta ‘noindex’ as well. But for excluding the external WooCommerce products unfortunately not (not sure what part of the code).

    Hopefully you can give me a new suggestion to test, and also let me know how i can regenerate the sitemap so i know for sure i can test the code.

    Hello,

    Thank you for providing additional details and for sharing the example of an external product on your site.

    It seems the previous code snippet did not effectively exclude external WooCommerce products from the XML sitemap.

    We can modify the filter to check if the products are external when generating the sitemap.

    Here’s an updated code snippet that you can use:

    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
        if ( 'product' === $type ) {
            $product = wc_get_product( $object->ID );
            if ( $product && $product->is_type( 'external' ) ) {
                return false;
            }
        }
        return $url;
    }, 10, 3 );

    If this method still doesn’t work, there might be specific nuances to your website setup that require a closer look.

    In such cases, providing temporary access to your site could be helpful for more in-depth troubleshooting.

    However, please ensure you have a complete backup of your site before sharing any sensitive access details.

    We really look forward to helping you.

    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 10 replies - 1 through 10 (of 10 total)

The ticket ‘Adding ‘noindex’ to external WooCommerce products’ is closed to new replies.