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 *

men s sexual wellness a review of leading options 2019 data reigniting desire exploring hormone therapy sexual wellbeing unbreakable strength the ultimate erection supplement boswellia cbd gummies natural pain relief top cbd thc gummies for anxiety relief find your calm cbd gummies for diabetes in canada benefits considerations kelly clarkson s transformative weight loss journey a real talk on health self discovery unveiling the keto gummies buzz separating fact from fiction your weight loss roadmap effective weight loss pills after gastric bypass surgery optimizing glycemic management a comprehensive guide to blood glucose diabetes control essential diabetes medications a comprehensive guide low carb diet effective blood sugar control strategy understanding what a high a1c level means for your health does blueberry lower blood sugar exploring the evidence and benefits normal blood glucose levels in adults clinical guidelines and interpretation melatonin and diabetes medications interactions impacts and management strategies diabetes medications in vials storage usage and best practices navigating a1c medication when to consider diabetes treatment abdominal and joint pain how prednisone can alleviate symptoms targeted relief cbd gummies for muscle joint discomfort