How to Hide WooCommerce Products Using ACF (Custom Field) Values
If you want to hide specific WooCommerce products based on custom field values or ACF (Advanced Custom Fields), it’s possible to customize the WooCommerce product query to achieve this. This guide walks you through the necessary steps to make this happen using simple code snippets.
Step 1: Install Necessary Plugins
Before we dive into the code, ensure you have the following plugins installed:
- WooCommerce: Required to manage your online store’s products.
- Advanced Custom Fields (ACF): Allows you to create custom fields for products, such as “Hide Product.”
Step 2: Create Custom Field for Products
You’ll need to create a custom field that will control the visibility of the product. You can do this by using the ACF plugin. Here’s how:
- Go to the ACF plugin settings in your WordPress admin panel.
- Create a new field group and add a new “True / False” field named
hide_product
or similar. - Assign the field to the “Product” post type, so it’s available for each WooCommerce product.
Step 3: Add Code to Functions.php
Once you’ve created the custom field, it’s time to modify the product query. Add the following code to your theme’s functions.php
file:
function custom_hide_products_based_on_acf_value($q) { if (!is_admin() && is_shop()) { $meta_query = $q->get('meta_query'); // Add the custom field condition to hide products $meta_query[] = array( 'key' => 'hide_product', // Replace 'hide_product' with your ACF field name 'value' => '1', 'compare' => '!=', // Show products where the 'hide_product' field is not equal to '1' ); $q->set('meta_query', $meta_query); } } add_action('woocommerce_product_query', 'custom_hide_products_based_on_acf_value');
Step 4: Test the Functionality
After adding the code to your functions.php
, products with the ‘hide_product’ field set to “1” will be hidden from your shop and archive pages. Test the functionality by setting this field to true for a product and visiting the shop page.
Step 5: Optional – Further Customization
If you want more advanced logic, such as hiding products based on multiple ACF fields, you can extend the meta_query
array. For example, you could add conditions for other fields like “category” or “stock status.” Here’s an example of how you can do that:
function custom_hide_products_based_on_acf_value($q) { if (!is_admin() && is_post_type_archive('product') && $q->is_main_query()) { $meta_query = $q->get('meta_query'); // Add the first custom field condition to hide products $meta_query[] = array( 'key' => 'hide_product', // Replace 'hide_product' with your ACF field name 'value' => '1', 'compare' => '!=', // Show products where the 'hide_product' field is not equal to '1' ); // Add more custom field conditions using OR relation $meta_query[] = array( 'key' => 'another_custom_field', // Replace with another custom field name 'value' => 'some_value', 'compare' => '=', // Show products where the 'another_custom_field' is equal to 'some_value' ); // Set the relation between the meta queries to 'OR' $meta_query['relation'] = 'OR'; $q->set('meta_query', $meta_query); } } add_action('woocommerce_product_query', 'custom_hide_products_based_on_acf_value');
Step 6: Save and Confirm Results
Once you’ve added the code and customized it according to your needs, make sure to save the changes. Test the shop page and product visibility to ensure that the products you want hidden are not showing up in the listings anymore.
Conclusion
By following this guide, you can easily hide products in WooCommerce based on ACF or custom field values. This solution is perfect for selectively displaying products to customers or managing visibility based on certain product attributes.
If you face any issues, always check the ACF field names and ensure the custom field values are being set correctly. With these adjustments, you’ll have full control over product visibility in your WooCommerce store.