How to Get All Product IDs in WooCommerce

How to Get All Product IDs in WooCommerce
On this page
  1. Why You'd Need a List of Product IDs
  2. Step 1: Get Every Published Product ID
  3. Step 2: Filter by Tag
  4. Troubleshooting
  5. Frequently Asked Questions
  6. Conclusion

You can get all product IDs in WooCommerce with the built-in wc_get_products() function by setting 'return' => 'ids', and the same function can filter that list down to a specific tag or category with one extra argument.

Why You’d Need a List of Product IDs

  • Powers custom loops, bulk calculations, or export scripts
  • Needed as input for other snippets that expect an array of product IDs
  • Useful for auditing catalog size or checking category-specific product counts
  • Avoids slower, more manual WP_Query setups for a simple ID list

Step 1: Get Every Published Product ID

PHP
/** Get every published product ID in the store */
$all_product_ids = wc_get_products( array(
    'limit'  => -1,
    'status' => 'publish',
    'return' => 'ids',
) );
Get all product IDs in WooCommerce: the result in a test store
Result in a test store

Step 2: Filter by Tag

PHP
/** Get product IDs that have the "clearance" tag */
$clearance_ids = wc_get_products( array(
    'limit'  => -1,
    'status' => 'publish',
    'return' => 'ids',
    'tag'    => array( 'clearance' ),
) );

Filtering by Category Instead

PHP
/** Get product IDs from the "outdoor-gear" category */
$outdoor_ids = wc_get_products( array(
    'limit'    => -1,
    'status'   => 'publish',
    'return'   => 'ids',
    'category' => array( 'outdoor-gear' ),
) );

Troubleshooting

The query returns far fewer IDs than expected

Confirm 'limit' => -1 is set — without it, WooCommerce applies the default query limit (often 10) and silently truncates the result.

Draft or out-of-stock products are missing

The status argument controls post status (publish/draft), not stock status. Add a separate 'stock_status' argument if you also need to include or exclude out-of-stock items specifically.

Frequently Asked Questions

Does this include variations, or only parent products?

By default it returns simple and variable parent product IDs, not individual variation IDs — query variations separately if you need those.

Can I combine tag and category filters together?

Yes, include both 'tag' and 'category' arguments in the same call, and WooCommerce will return products matching both conditions.

Is wc_get_products() slower than a direct database query?

It’s slightly slower than raw SQL but far more reliable, since it respects WooCommerce’s internal caching and object structure — worth the small tradeoff for most use cases.

Conclusion

The wc_get_products() function is the safest way to get all product IDs in WooCommerce, whether you need the entire catalog or a filtered subset by tag or category. Keep 'return' => 'ids' in mind whenever you only need identifiers instead of full product objects, since it’s noticeably lighter on memory.

See also How to Get a List of All Customers in WooCommerce. Then read How to Limit Product Search on WooCommerce Manual Admin Orders.

Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.

Leave a Reply

Schedule a call