In this post I want to share a quick WooCommerce hide product from search results snippet that you can add to the theme or a plugin to help hide any product you wish to hide from search results. This code snippet uses the same approach as the previous tutorial on how to hide WooCommerce product without price.
If you want to hide specific product from the search results, you should consider adding this snippet to your theme and set the product visibility to hidden since the snippet uses the value of the visibility in a meta query to hide the product from search.
WooCommerce Hide Product from Search Results
To hide products from search you should add this code snippet to the functions.php of your theme or you can add this code the plugin files if you want to hide product from search using a custom plugin.
if ( ! function_exists( 'njengah_hide_products_from_search' ) ){ function njengah_hide_products_from_search( $query = false ) { if(!is_admin() && is_search()){ $query->set( 'meta_query', array( 'relation' => 'OR', array( 'key' => '_visibility', 'value' => 'hidden', 'compare' => 'NOT EXISTS', ), array( 'key' => '_visibility', 'value' => 'hidden', 'compare' => '!=', ), )); } } } add_action( 'pre_get_posts', 'njengah_hide_products_from_search' );
After adding this code snippet you should also enure the product you want to hide should have the visibility set to hidden.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Resize WooCommerce Product Images
- How to Set Custom Related Products In WooCommerce
- How to Hide Out of Stock Products in WooCommerce
- How to Add Remove Icon to WooCommerce Checkout Page
- How to Hide a Product in WooCommerce or Hide Products by Category or Roles
- How to Change Product Price Programmatically in WooCommerce
- How to Set Featured Products In WooCommerce
- How to Redirect On Refresh WordPress Page » Detect Page Refresh PHP
- How to Remove Has Been Added to Your Cart Message WooCommerce
- How to Hide the WordPress Admin Bar Programmatically Without Plugin
- WooCommerce Redirect After Checkout : Redirect to Custom Thank You Page
- How to Hide All Products From Shop Page in WooCommerce
- How to Add Custom Shipping Method in WooCommerce
- How to Fix Yoast SEO Title not Working – Fixing Duplicate Title Tags
- How to Add a Trust or Secure Logo on WooCommerce Checkout Page