WooCommerce URLs often contain query parameters like ?add-to-cart= and ?filter_color= which can create duplicate content and bloat your SEO index. To prevent search engines from indexing such URLs, it’s a good practice to add a noindex meta tag dynamically when these parameters are detected.
Here’s a simple way to do it using a custom function in WordPress.
Step-by-Step: Add Noindex Meta Tag for Add-to-Cart & Filter URLs
- Open your theme’s
functions.phpfile
located in your child or main theme folder - Add the following code:
function wp_noindex_unwanted_urls() {
// Noindex for Add to Cart URLs
if (isset($_GET[‘add-to-cart’])) {
echo ” . “\n”;
return;
}
// Noindex for Filter and Sorting Parameters
$unwanted_params = array(
'filter_',
'query_type_',
'min_price',
'max_price',
'rating_filter',
'orderby'
);
foreach ($_GET as $key => $value) {
foreach ($unwanted_params as $param) {
if (stripos($key, $param) !== false) {
echo '<meta name="robots" content="noindex, follow">' . "\n";
return;
}
}
}
}
add_action(‘wp_head’, ‘wp_noindex_unwanted_urls’);
Category:
