Need every completed order from last month, or every order for one customer? Looping over all orders is slow and wastes memory. WooCommerce has a query class for this: WC_Order_Query.
It takes an array of filters, runs them against the order data store, and returns matching order objects. This guide covers the syntax, the query variables you will use most, pagination, and the two hooks that change query results.
What WC_Order_Query Does
WC_Order_Query builds an order query from an array of arguments such as status, customer, creation date, and payment method. Its get_orders() method runs the query and returns the matches as WC_Order objects, so you can call the same getters you use elsewhere in WooCommerce.
The wc_get_orders() function is a shortcut for simple cases. It creates a WC_Order_Query behind the scenes. Use the class directly when you want to hold the query object, adjust it step by step, or read pagination data.
Tip: Use
wc_get_orders()for one-off queries. Switch toWC_Order_Querywhen you want to change the query object before running it.
Basic Syntax
Create the Query
Pass the arguments to the constructor. Each key is a query variable, and each value sets the filter.
<?php
$query = new WC_Order_Query(
array(
'limit' => 10,
'status' => array( 'wc-completed' ),
'orderby' => 'date',
'order' => 'DESC',
)
);
$orders = $query->get_orders();Run the Query With get_orders()
get_orders() returns an array of WC_Order objects. Loop over the array and call the getters you need. The loop below prints the ID and total of each order:
<?php
foreach ( $orders as $order ) {
printf( "Order #%d: %s\n", $order->get_id(), $order->get_total() );
}Common Query Variables
These variables cover most queries:
| Variable | Purpose | Example |
|---|---|---|
status | Order statuses, using keys from wc_get_order_statuses() | array( 'wc-processing', 'wc-completed' ) |
customer_id | Orders placed by one customer account | 25 |
date_created | Creation date or a date range | '2026-10-01...2026-10-31' |
payment_method | Payment gateway ID | 'stripe' |
limit | Maximum orders returned, or -1 for all | 20 |
paginate | Return an object with the orders and totals | true |
return | Return WC_Order objects (default) or only IDs | 'ids' |
Status keys carry the wc- prefix, so use wc-completed. A bare completed does not match the stored status. Combine variables in one array. This example returns one customer’s completed orders from October 2026:
<?php
$query = new WC_Order_Query(
array(
'customer_id' => 25,
'date_created' => '2026-10-01...2026-10-31',
'status' => array( 'wc-completed' ),
)
);
$orders = $query->get_orders();Paginate Results
Set paginate to true and the query returns an object instead of an array. The object holds the orders for the current page, the total count, and the page count, so you can build a pager without a second query.
<?php
$query = new WC_Order_Query(
array(
'limit' => 20,
'page' => 2,
'paginate' => true,
)
);
$results = $query->get_orders();
printf( "Total: %d, pages: %d\n", $results->total, $results->max_num_pages );
foreach ( $results->orders as $order ) {
printf( "Order #%d\n", $order->get_id() );
}Filter Query Arguments With Hooks
WooCommerce runs two filters around every WC_Order_Query. One changes the arguments before the query runs. The other changes the results after it runs.
Change the Query Arguments
woocommerce_order_query_args receives the query variables before the query runs. This example adds a 30-day window only when the caller did not set a date:
<?php
add_filter( 'woocommerce_order_query_args', 'my_recent_orders_only' );
function my_recent_orders_only( $args ) {
if ( empty( $args['date_created'] ) ) {
$args['date_created'] = '>' . ( time() - 30 * DAY_IN_SECONDS );
}
return $args;
}Change the Results
woocommerce_order_query receives the results after the query runs. This example drops free orders from non-paginated results. It filters the output in PHP, so the database still returns those rows.
<?php
add_filter( 'woocommerce_order_query', 'my_skip_free_orders' );
function my_skip_free_orders( $results ) {
if ( ! is_array( $results ) ) {
return $results;
}
return array_filter(
$results,
function ( $order ) {
return $order->get_total() > 0;
}
);
}Common Mistakes
Most query bugs come from a few patterns:
- Wrong status keys. Use the keys from
wc_get_order_statuses(), such aswc-completed. - Looping over the wrong value. With
paginateset totrue, loop over$results->orders, not$results. - Skipping error handling.
get_orders()can throw an exception when data store validation fails. Wrap the call intryandcatchin production code.
FAQs About WC_Order_Query
Is WC_Order_Query the Same as wc_get_orders()?
Not quite. wc_get_orders() is a function that creates a WC_Order_Query, runs it, and returns the orders. Use the class when you need the query object itself.
Does It Work With HPOS?
Yes. The query goes through WooCommerce’s order data store, so the same arguments work with High-Performance Order Storage and with legacy post-based storage. See the HPOS guide for how the two storage modes differ.
How Do I Count Orders Without Loading Them?
Set paginate to true and read $results->total. You can also set return to 'ids' and count the array. Both still run the query, but the 'ids' option skips building WC_Order objects.
Conclusion
WC_Order_Query gives you one syntax for filtering orders by status, customer, date, and payment method. Start with the variables in the table, then add pagination or a filter when the case calls for it.
Need custom order reports or order tools for your store? Hire a WooCommerce developer, or reach out to me directly.
