Query WooCommerce orders where meta data does not exist

The meta_query argument (that you can use in a WP_Query) is not enabled by default when using a WC_Order_Query through wc_get_orders() WooCommerce function.

But for you can use the undocumented Custom Field Parameters (just like in a WP_Query):

  • meta_key
  • meta_value
  • meta_value_num
  • meta_compare

So in your case you can use the following instead:

$orders = wc_get_orders( array(
    'limit'        => -1, // Query all orders
    'orderby'      => 'date',
    'order'        => 'DESC',
    'meta_key'     => 'var_rate', // The postmeta key field
    'meta_compare' => 'NOT EXISTS', // The comparison argument
));

This time it works nicely (Tested on WooCommerce 3.5.8 and 3.6.2).

Leave a Comment