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 *

fluffy wins casino resena versailles casino resena wicked jackpots casino resena viperwin casino es 2025 review casino retiro sin deposito ts times square casino resena mx codere leon vs rayados es juegos slots gratis penalty shoot out casinos con maquinas tragamonedas para jugar gratis jugar tragaperras con dinero real fortunica casino resena como ganar en juegos de casino online 38667 electric nights pon tu ritmo en la pista de baile bingo en jaca la laguna casino codigo promocional y bonus code fbet casino cz resena slot 888 bonanza juego fenix play deluxe 2 juega gratis a jacks or better double up en modo demo hay casino en valeria del mar kike lago se hace con 80 000e en un torneo de poker introduccion al blackjack juegos de blackjack gratis sin descargar 67607 mx caliente promocion casino jueves es mx online casinos