In WooCommerce, finding all orders that include a specific product can be trickier than it sounds—especially with the new High-Performance Order Storage (HPOS) system. Since HPOS replaces the old posts and postmeta tables with custom ones, direct database queries are no longer the recommended approach. Instead, we should rely on the official CRUD functions and helpers like wc_get_orders().
Unfortunately, the official documentation doesn’t cover this particular use case at all. There’s no example showing how to fetch orders that contain a given product, which leaves many developers guessing or resorting to outdated SQL queries.
In this snippet, you’ll see a clean, efficient way to retrieve WooCommerce orders based on a product ID using wc_get_orders(). It’s fully compatible with HPOS, safe to use in any environment, and easy to adapt.

