WC_Order_Query where meta data does not exist or is not equal to
-
I am trying to query all orders from the database where a custom post meta field is not set, or is not equal to ‘yes’. It seems I am not able to get it to work properly.
I have this:
$query = new \WC_Order_Query([ 'date_completed' => $range, 'x_days_follow_up_email' => 'yes' // Note: Compares '!=' ]);Then I have this hooked into
woocommerce_order_data_store_cpt_get_orders_query:if (!empty($query_vars['x_days_follow_up_email'])) { $query['meta_query'][] = [ 'key' => 'x_days_follow_up_email', 'value' => esc_attr($query_vars['x_days_follow_up_email']), 'compare' => '!=' ] ]; } return $query;This returns nothing. If I change the compare to
==, it returns the orders with the meta set to ‘yes’. So I was thinking I might have to includeNOT EXISTSsomehow, like this:if (!empty($query_vars['x_days_follow_up_email'])) { $query['meta_query'][] = [ 'relation' => 'OR', [ 'key' => 'x_days_follow_up_email', 'value' => esc_attr($query_vars['x_days_follow_up_email']), 'compare' => '!=' ], [ 'key' => 'x_days_follow_up_email', 'compare' => 'NOT EXISTS' ] ]; } return $query;This sadly does not work either. I could write my own WP_Query for this, but there are a lot of orders and I find that the query takes ages. The
$rangein my code is basically all orders that are 11 – 8 days old. And I only want the orders that do not have the metax_days_follow_up_email, or when that meta is set but not toyes.It seems that it should be possible? For what it’s worth, this was working before, but it suddenly stopped working after an update for reasons unknown:
$query = new \WC_Order_Query([ 'date_completed' => $range, 'meta_key' => 'x_days_follow_up_email', 'meta_value' => 'yes', 'meta_compare' => '!=' ]);Seems simple enough, but I can’t seem to make it happen.
The topic ‘WC_Order_Query where meta data does not exist or is not equal to’ is closed to new replies.