I found myself in the situation where I had to give the client the possibility to hide some of their products from search, so they can manually send the product urls to their clients. The way I got around it at the time was to create a product category called Hidden and use the snippet below, which you can paste in your theme’s functions.php file.

/*-------------------------------------
  Remove hidden category products from search
---------------------------------------*/
function wpharvest_pre_get_posts( $query ) {
   if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
       $tax_query = array(
           array(
               'taxonomy' => 'product_cat',
               'field'   => 'slug',
               'terms'   => 'hidden',
               'operator' => 'NOT IN',
           ),
       );
       $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'wpharvest_pre_get_posts' );

If you find this snippet helpful or need help with it, please leave a comment below.

Published by Dragos Micu

WooCommerce

Leave a Reply

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

cbd gummies for enhanced relaxation intimacy how long before extenze shows results counterfeit erectile dysfunction supplements a buyer beware guide gentle relief managing arthritis pain with cbd gummy bears unlock the full potential of cbd thc reserve gummies reviews revealed unlock the potential your complete guide to 300mg cbd gummies managing stomach discomfort with cbd gummies a user s guide phendimetrazine examining the risks and benefits of this weight loss medication kelly clarkson s keto gummies the ultimate guide to unlocking secrets boosting energy and losing weight the unfiltered truth lana del rey s 50 pound weight loss and transformation revealed emerging therapies exploring new weight management medications does meat lower blood sugar the surprising truth behind the myth most common diabetes medications a comprehensive overview is 111 mg dl blood sugar too high understanding normal ranges and health implications managing blood sugar the role of hydration etiologies of pain extending from ankle to knee a medical overview artisan joint relief complex how to manage inner knee pain from running expert tips for prevention and recovery joint revival a comprehensive guide to pain management and restoring mobility collagen for joint pain hype or hope