I’m trying to get all the shop orders with a meta key "order_referrer_id" and the value will be a user id for example "1060".
This is what i have tried so far:
$args = array(
'limit' => -1,
'status' => 'completed',
'meta_query' => array(
array(
'key' => 'order_referrer_id',
'value' => 1060,
'compare' => '='
),
),
'return' => 'ids',
);
$orders = wc_get_orders( $args );
For some reason the query ignore the meta query and returns all the orders.
What is the correct way to filter shop orders?
2
Answers
wc_get_orders
andWC_Order_Query
provide a standard way of retrieving orders that is safe to use and will not break due to database changes in future WooCommerce versions.Source: wc_get_orders and WC_Order_Query
You can use
meta_key
,meta_value
andmeta_compare
, so you get:Cleanest way to do this, is to create a custom parameter which you can use to create your WC_Order_Query. As mentioned in the docs here: https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#adding-custom-parameter-support
Then you can use it like this: