Hide WooCommerce Products Using ACF Field Logic

If you’d like to conditionally hide WooCommerce products using custom field values created with ACF (Advanced Custom Fields), you’re in luck. In this guide, we’ll show how to filter WooCommerce queries using ACF fields to control product visibility.

Step 1: Install Required Plugins

Before proceeding, make sure you have these installed:

  • WooCommerce – Essential for managing your products.
  • Advanced Custom Fields (ACF) – Lets you add and manage custom fields such as “Hide Product.”

Step 2: Create a Custom ACF Field

Set up a field to determine if a product should be hidden:

  1. Open ACF settings in your WordPress admin dashboard.
  2. Create a new field group with a “True / False” field called hide_product.
  3. Apply the group to the Product post type.

Step 3: Add Query Filter in functions.php

Add this snippet to your theme’s functions.php file to modify WooCommerce queries:

function custom_hide_products_based_on_acf_value($q)
{
    if (!is_admin() && is_shop()) {
        $meta_query = $q->get('meta_query');

        $meta_query[] = array(
            'key'     => 'hide_product',
            'value'   => '1',
            'compare' => '!=',
        );

        $q->set('meta_query', $meta_query);
    }
}
add_action('woocommerce_product_query', 'custom_hide_products_based_on_acf_value');

Step 4: Verify Product Visibility

Set the hide_product field to “true” for any product you want hidden. These will no longer show on shop/archive pages.

Step 5: Optional Advanced Conditions

You can also combine multiple ACF fields using this structure:

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');

        $meta_query[] = array(
            'key'     => 'hide_product',
            'value'   => '1',
            'compare' => '!=',
        );

        $meta_query[] = array(
            'key'     => 'another_custom_field',
            'value'   => 'some_value',
            'compare' => '=',
        );

        $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 & Test

Once the code is in place, update your products with the custom field values. Hidden products will no longer appear in listings.

Conclusion

This approach allows you to control product visibility in WooCommerce using ACF fields. It’s ideal for hiding specific products based on any condition you define with custom fields.


Additional Resources

Leave a Comment

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

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.