No index hidden WooCommerce products from Yoast

WooCommerce Products with the visibility status hidden (excluded from catalog) are still indexed by the Yoast SEO tool. TO remove them from the automatically generated Sitemaps and set the index status to “noindex, nofollow” add the following 2 functions to your functions.php

add_filter( 'wpseo_sitemap_entry', 'we_remove_hidden_products', 99, 3);
function we_remove_hidden_products($url, $type, $post) {
	if($post->post_type == 'product' && is_object_in_term( $post->ID, 'product_visibility', 'exclude-from-catalog')){
		return '';
	}
	return $url;
}

add_filter('wpseo_robots', 'we_noindex_hidden_products', 10, 2);
function we_noindex_hidden_products($robots, $indexable) {
	
	if(!isset($indexable->model)) {
		return $robots;
	}

	if(!isset($indexable->model->object_id)) {
		return $robots;
	}
	
	if(is_object_in_term( $indexable->model->object_id, 'product_visibility', 'exclude-from-catalog')){
		$robots = 'noindex, nofollow';

	}
	return $robots;
}

2 thoughts on “No index hidden WooCommerce products from Yoast

  1. Gerardo says:

    Hello Daniel,
    Thanks for posting this! I was wondering if this functions will work with any of the SEO plugins out there, or just Yoast?

    Thank you,
    Gerardo

  2. Stas says:

    Hello!
    What is the correct way to rewrite a function for use with $meta_query?
    For example, to use “_stock_status” instead of “product_visibility”

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.

Close Popup
Privacy Settings saved!
Privacy Settings

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. Control your personal Cookie Services here.

These cookies are necessary for the website to function and cannot be switched off in our systems.

Technical Cookies
In order to use this website we use the following technically required cookies
  • wordpress_test_cookie
  • wordpress_logged_in_
  • wordpress_sec

Decline all Services
Save
Accept all Services