Use ACF to Hide Products in WooCommerce

ACF ACF Option Page ACF Tutorials Advanced Guides Woocommerce WooCommerce Custom Code Woocommerce Hooks WooCommerce Tips Wordpress WordPress Development WordPress Functions

Use ACF to Hide Products in WooCommerce Tutorial/Guide

Looking to hide WooCommerce products using ACF custom field values? With a small tweak to your product query, you can control visibility via your custom fields.

Step 1: Required Plugins

Install these plugins before starting:

  • WooCommerce: Manages the product listing and shop structure.
  • Advanced Custom Fields: Lets you assign a hide_product field to products.

Step 2: Add the ACF Field

Here’s how to configure the field in ACF:

  1. Create a new Field Group in ACF settings.
  2. Add a “True / False” field with the name hide_product.
  3. Set the field group location to Products (post type).

Step 3: Filter the Product Query

Add the following code to your functions.php file:

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

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

        $query->set('meta_query', $meta_query);
    }
}
add_action('woocommerce_product_query', 'hide_products_with_acf_flag');

Step 4: Test Product Visibility

Mark any product with the hide_product field as true, and it will be excluded from the shop page listings.

Step 5: Add More Field Conditions

Want to filter based on multiple ACF fields? Use this enhanced version:

function hide_products_with_multiple_acf_conditions($query)
{
    if (!is_admin() && is_post_type_archive('product') && $query->is_main_query()) {
        $meta_query = $query->get('meta_query');

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

        $meta_query[] = array(
            'key'     => 'custom_flag',
            'value'   => 'yes',
            'compare' => '=',
        );

        $meta_query['relation'] = 'OR';

        $query->set('meta_query', $meta_query);
    }
}
add_action('woocommerce_product_query', 'hide_products_with_multiple_acf_conditions');

Conclusion

Using ACF and a few lines of PHP, you can filter WooCommerce products dynamically. Perfect for seasonal or membership-based product visibility management.

Helpful Links:

💡 Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.