How to Create a Custom Order Filter in WooCommerce Admin
In this guide, I am going to show you how you can easily create a custom order filter for the WooCommerce > Orders page, which will work great for both CPT-based and HPOS orders.
In order to keep this tutorial as simple as possible we’re going to create a filter just by a single custom field value. This is how it is going to look:

Step 1. Add a Field to the Order Filter Form
At this very first step, we’re just going to add a select dropdown field before the “Filter” button here:

And we can do it with the help of one of the following hooks:
woocommerce_order_list_table_restrict_manage_orders– for HPOS orders,restrict_manage_posts– for CPT-based orders.
What is great about these two hooks is that the first parameter in both of them is a custom post type name! Yes, it is made in terms of backward compatibility and basically it allows you to use a single function for both of the hooks at the same time, let me show you how:
<?php
add_action( 'woocommerce_order_list_table_restrict_manage_orders', 'rudr_order_filter', 25, 2 );
add_action( 'restrict_manage_posts', 'rudr_order_filter', 25, 2 );
function rudr_order_filter( $post_type, $which ) {
if( 'shop_order' !== $post_type ) {
return;
}
$rudr_custom_field = isset( $_GET[ 'rudr_custom_field' ] ) ? $_GET[ 'rudr_custom_field' ] : '';
?>
<select name="rudr_custom_field">
<option value="">Custom field</option>
<option value="value-1"<?php selected( $rudr_custom_field, 'value-1' ) ?>>Value 1</option>
<option value="value-2"<?php selected( $rudr_custom_field, 'value-2' ) ?>>Value 2</option>
</select>
<?php
}Nothing to explain here, just don’t forget to provide a name HTML attribute for your form field and please pay attention to the line 11, we need it to prevent PHP notice from happening.
The $which argument value can be either top or bottom.
Step 2. Filtering the Orders
Great, the field is added, but of course, it doesn’t work at this moment! That’s not always the case, by the way, but in our situation, we need to filter orders with another hook.
Here, we have two hooks again, and we need to choose one depending on whether we’re using high-performance order storage or not (or use both of them if you need to maintain backward compatibility).
woocommerce_order_list_table_prepare_items_query_args– for HPOS orders,pre_get_posts– this one should be familiar for you, it will work great for classic orders.
The first hook example:
add_action( 'woocommerce_order_list_table_prepare_items_query_args', function( $query_args ) {
if( isset( $_GET[ 'rudr_custom_field' ] ) && $_GET[ 'rudr_custom_field' ] ) {
$query_args[ 'meta_query' ] = array(
array(
'key' => 'rudr_custom_field',
'value' => $_GET[ 'rudr_custom_field' ],
)
);
}
return $query_args;
} );The second hook example:
add_action( 'pre_get_posts', function( $query ) {
if( ! is_admin() ) {
return;
}
global $pagenow;
// just being super-accurate here
if( 'edit.php' !== $pagenow || 'shop_order' !== $query->get( 'post_type' ) ) {
return;
}
if( isset( $_GET[ 'rudr_custom_field' ] ) && $_GET[ 'rudr_custom_field' ] ) {
// also works
// $query->set( 'meta_key', 'rudr_custom_field' );
// $query->set( 'meta_value', $_GET[ 'rudr_custom_field' ] );
$query->set( 'meta_query', array(
array(
'key' => 'rudr_custom_field',
'value' => $_GET[ 'rudr_custom_field' ],
)
) );
}
} );As you can see, the code is kind of similar, so maybe you will find a way to put the same PHP function on both hooks as we did in the first example at the beginning of the tutorial.
Example. Filter WooCommerce Orders by Product
Great, now we know how to create WooCommerce filters for orders. Let’s try to implement one more example – to create a WooCommerce order filter by product.
Creating a product dropdown list
First things first, we need to create a select dropdown on the WooCommerce > Orders page. We’re going to do it supporting both HPOS and CPT-based orders, of course.
So here you go:
<?php
// display a product select dropdown
add_action( 'woocommerce_order_list_table_restrict_manage_orders', 'rudr_product_select', 25 );
add_action( 'restrict_manage_posts', 'rudr_product_select', 25 );
function rudr_product_select( $post_type ) {
if( 'shop_order' !== $post_type ) {
return;
}
$query = new WC_Product_Query( array(
'limit' => -1,
'orderby' => 'name',
'order' => 'ASC',
) );
$products = $query->get_products();
if( ! $products ) {
return;
}
$selected_product_id = isset( $_GET[ 'filter_by_product' ] ) && $_GET[ 'filter_by_product' ] ? absint( $_GET[ 'filter_by_product' ] ) : 0;
?>
<select name="filter_by_product">
<option value="">Filter by product</option>
<?php foreach( $products as $product ) : ?>
<option value="<?php echo $product->get_id() ?>" <?php selected( $product->get_id(), $selected_product_id ) ?>><?php echo $product->get_name() ?></option>
<?php endforeach; ?>
</select>
<?php
}Just a couple of things I would like you to keep in mind:
- Printing the products right away into the
<select>HTML tag usingWC_Product_Queryis ok, but only when your shop is not going to have hundreds of them. If your WooCommerce really does have a lot of products, I would recommend using select2 (selectWoo) script for this purpose; you can see the whole implementation in my Multisite Order Sync plugin. WC_Product_Queryalso returns an array of product objectsWC_Product, so feel free to use methods likeget_id(),get_name(), etc.
Here is what it is going to look like in my case:

Filtering orders by product (both HPOS and CPT-based orders)
Now, let’s continue to the filtering part, which is going to be much more challenging, not only because we need to use different functions for HPOS and CPT-based orders but also because we can’t filter orders by simply providing one more query parameter, we have to modify the SQL query itself. In order to do so we’re going to use two filter hooks.
woocommerce_orders_table_query_clauses – for HPOS orders:
// filtering orders (HPOS)
add_filter( 'woocommerce_orders_table_query_clauses', function( $clauses ) {
global $wpdb;
$selected_product_id = isset( $_GET[ 'filter_by_product' ] ) && $_GET[ 'filter_by_product' ] ? absint( $_GET[ 'filter_by_product' ] ) : 0;
if( ! $selected_product_id ) {
return $clauses;
}
$clauses[ 'where' ] .=
"
AND {$selected_product_id}
IN (
SELECT itemmeta.meta_value
FROM {$wpdb->prefix}woocommerce_order_items as items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as itemmeta
ON itemmeta.order_item_id = items.order_item_id
WHERE items.order_item_type = 'line_item'
AND itemmeta.meta_key = '_product_id'
AND {$wpdb->prefix}wc_orders.id = items.order_id
)
";
return $clauses;
}, 25 );posts_where – for CPT-based orders:
// Filtering orders (CPT-based orders)
add_filter( 'posts_where', function( $where, $query ) {
global $wpdb;
if( $query->is_search() ) {
return $where;
}
$selected_product_id = isset( $_GET[ 'filter_by_product' ] ) && $_GET[ 'filter_by_product' ] ? absint( $_GET[ 'filter_by_product' ] ) : 0;
if( ! $selected_product_id ) {
return $where;
}
$where .=
"
AND {$selected_product_id}
IN (
SELECT itemmeta.meta_value
FROM {$wpdb->prefix}woocommerce_order_items as items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as itemmeta
ON itemmeta.order_item_id = items.order_item_id
WHERE items.order_item_type = 'line_item'
AND itemmeta.meta_key = '_product_id'
AND {$wpdb->posts}.ID = items.order_id
)
";
return $where;
}, 25, 2 );
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hey, thanks for sharing your knowledge! I can’t seem to be able to work with the first hook example: it adds correctly the key:value but WP seems not to care about it.. default filters seem not to work with it neither.. any help?
Great Post, thanks. Is there a way to filter orders which have a shipping_total_amount greater than 0?
You’re welcome!
Yes, absolutely, though I don’t have a ready code for you…
Hey thanks it was really useful but I think there’s a little error: you first say to use the
woocommerce_order_data_store_cpt_get_orders_queryhook to filter orders (which didn’t work for me) before usingwoocommerce_order_list_table_prepare_items_query_argsin the example (which I think is the correct one).Hey Alva,
thanks for noticing!
Great Blog! Keep it up! Share any similar for Product Page Custom Filters if exists, thanks!
Thank you Konstantinos!
What specific filters would you like me to describe for the product page?