Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Blog Categories  Tips & Tricks Contact

HOME WOOCOMMERCE HOW TO HIDE SPECIFIC PRODUCTS FROM SHOP PAGE AND SEARCH
RECENT POSTS

PAGE IN WOOCOMMERCE? How to Create a Custom Widget in


WordPress?

How to Hide Speci c Products from Shop How to Hide Speci c Products
from Shop Page and Search Page
Page and Search Page in WooCommerce? in WooCommerce?

HOW TO WOOCOMMERCE How to Use Where Condition in


Laravel Eloquent?
AMAN MEHRA MAY 27, 2021 LEAVE A COMMENT
How to Install Laravel 8
Tweet on Twitter Share on Facebook Framework on Windows, macOS
and Linux?

How to Add WooCommerce Cart


Icon in Menu with Item Count?

CATEGORIES

How To

Laravel

PHP

ReactJS

WooCommerce

In this tutorial, we will learn how to hide speci c products from woocommerce WordPress

shop page with just a simple hook. We will make a hook to get the product’s
query then we will customize it and then return it with our customization.
MOST VIEWED POSTS
You can also hide products based on any speci c condition. Like check
WooCommerce Remove
products in stock, outstock, or have any attributes, variations, etc. You can make Update Cart Button and
Make it Automatically
it as per your requirement.
Update.
October 5, 2020
Let’s start with it.
How to Change Price of
Speci c Product and
Hide Speci c Products From Shop Page Quantity in Cart?
October 14, 2020

To hide the speci c product from the woocommerce shop page, we will use How to Redirect to
woocommerce_product_query_meta_query lter hook. We will add our Checkout After Add to
Cart?
own custom query as a meta query in my case. You can do it like you want. October 16, 2020

OK. Add the following code in your functions.php le and save it. It will hide the How can I Prevent SQL
Injection in PHP?
out-of-stock products from your shop page. October 7, 2020

1 add_filter( 'woocommerce_product_query_meta_query', 'hid


Upload Multiple Featured
2 Images in a Post OR Page
3 function hide_out_of_stock_products_from_shop( $meta_que January 4, 2021
4
5 if( ! is_shop() || is_admin() || is_search() ) retur
6
7 $meta_query[] = array(
TIPS & TRICKS
8 'key' => '_stock_status',
9 'value' => 'outofstock', Create Laravel Model, Migration,
10 'compare' => '!=' Controller, and Resource in One
11 ); Command
12 return $meta_query;
Copy to Clipboard with JavaScript
13 – (also for hidden element)
14 }
How to Change Excerpt Length in
WordPress
Hide Speci c Products From Search Page
Open All External Links in New
Window
There is another code hook for the search page. If you also want to hide speci c
products from the search page then you have to add the following code in your QR Code Generate With Python
functions.php le.
FOLLOW US
Stay updated via social channels
To hide from the search page we will use pre_get_posts action hook. In
this hook, we will rst get the meta_query object then we added our own query
then again set the meta_query. Same as we did above to add your own custom
meta query in the existing query.

See the code below.

1 add_action( 'pre_get_posts', 'hide_out_of_stock_products


2
3 function hide_out_of_stock_products_from_search( $query
4
5 if( $query->is_search() && $query->is_main_query()
6 $meta_query = (array) $query->get( 'meta_query'
7
8 $meta_query[] = array(
9 'key' => '_stock_status',
10 'value' => 'outofstock',
11 'compare' => '!='
12 );
13
14 $query->set( 'meta_query', $meta_query );
15 }
16
17 }

These are programmatic ways to hide speci c products from the shop page (in
my case, we hide the out-of-stock products). But you can make any type of
condition with your custom eld value that you have built with ACF or your own
code. You can compare your value and show the result accordingly.

Additionally, If you want to hide out-of-stock products from woocommerce


setting page then check the below image.

You have to visit this path WooCommerce > Settings > Products > Inventory as
showing in the image and you will nd the checkbox to enable/disable for out-
of-stock products.

More WooCommerce Tutorials


WooCommerce Remove Update Cart Button
Change Price of Speci c Product in Cart
Redirect to Checkout after Click on Add to Cart Button

Hope you understand the code above, If you still have any questions ask me in
the comment section. I’ll respond to you as soon as possible. Let me know if
want more tutorial like this another condition, I’ll make it for you.

PRE_GET_POSTS SEARCH PAGE SHOP PAGE WOOCOMMERCE CUSTOMIZATION


WOOCOMMERCE_PRODUCT_QUERY_META_QUERY WOOCOMMERCE. PRODUCT WORDPRESS
WORDPRESS WOOCOMMERCE
Tweet on Twitter Share on Facebook

YOU MAY ALSO LIKE

How to Create a Custom Widget in How to Use Where Condition in


WordPress? Laravel Eloquent?

How to Install Laravel 8 Framework How to Add WooCommerce Cart Icon


on Windows, macOS and Linux? in Menu with Item Count?

How to Secure Password Using How to Make a DataTable in


bcrypt in PHP? ReactJS?

ABOUT THE AUTHOR: AMAN MEHRA


Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+
years of experience. I love coding and help to people with this blog.

LEAVE A REPLY

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

Comment

Name * Email * Website

Save my name, email, and website in this browser for the next time I comment.

POST COMMENT

ABOUT QUICK LINKS RECENT POSTS JOIN OUR NEWSLETTER

Your Blog Coach is the best site Blog How to Create a Custom Widget in Name
for nding the solution to any WordPress?
WordPress
issue related to coding and learn
How to Hide Speci c Products
more cool stuff and tricks. WooCommerce Email
from Shop Page and Search Page
Contact in WooCommerce?

How to Use Where Condition in


SUBSCRIBE
Laravel Eloquent?
© 2020 Your Blog Coach Privacy Policy Terms and Conditions Sitemap

You might also like