Correct code to hide Woocommerce Discontinued products from Woocoommerce search results & “Related Products”
I use a plugin for Woocommerce Discontinued products (which appears to be unsupported and nobody responds to anyone). The products that no longer exist that we have marked as discontinued keep showing up in "search results", in "related products", and in the shop. We don’t want them to show up so we used a code that someone had posted in the support page for the plugin.
The code was:
// Hide Discontinued Products
add_filter( 'woocommerce_product_query_meta_query', 'show_only_products_with_not_discontinued_meta', 10, 2 );
function show_only_products_with_not_discontinued_meta( $meta_query, $query ) {
// Only on shop pages
if( ! is_shop() ) return $meta_query;
$meta_query[] = array(
'key' => '_is_discontinued',
'compare' => 'NOT EXIST'
);
return $meta_query;
}
The problem with the code is that it hides every single active product that we have ever imported using the Woocommerce product import tool from showing up in searches (More than 5,000 products stopped appearing in search results). So, we deleted the code and are back to square one.
How can we hide only discontinued products from search and related products? We want to keep the product pages, as there are 300+ of them and they all rank extremely well for SEO, but we won’t want customers to see them when they use the website.
Plugin:
https://github.com/twoelevenjay/woocommerce-discontinued-products
https://wordpress.org/plugins/discontinued-products/
Discontinued product example:
https://djdeals.com/product/faderfox-pc44-universal-controller-ableton-live/
Leave an answer