Dynamic Visibility: Using ACF to Exclude WooCommerce Products

Managing a large catalog often requires hiding specific items from the main shop pageβ€”whether they are seasonal items, members-only products, or components of a bundle. While WooCommerce offers basic visibility settings, using Advanced Custom Fields (ACF) provides a much more intuitive workflow for site managers. By adding a simple "True/False" toggle to your product editor, you can programmatically alter the main shop query to skip products flagged for hiding, keeping your storefront organized without deleting inventory.

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.