WC_Order_Query Class in WooCommerce: Examples [Guide]

WC_Order_Query class with WooCommerce order objects

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 to WC_Order_Query when 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:

VariablePurposeExample
statusOrder statuses, using keys from wc_get_order_statuses()array( 'wc-processing', 'wc-completed' )
customer_idOrders placed by one customer account25
date_createdCreation date or a date range'2026-10-01...2026-10-31'
payment_methodPayment gateway ID'stripe'
limitMaximum orders returned, or -1 for all20
paginateReturn an object with the orders and totalstrue
returnReturn 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 as wc-completed.
  • Looping over the wrong value. With paginate set to true, loop over $results->orders, not $results.
  • Skipping error handling. get_orders() can throw an exception when data store validation fails. Wrap the call in try and catch in 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.

About The Author

Picture of Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

Kinsta - Managed Hosting for WordPress

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.