• Resolved Dekadinious

    (@dekadinious)


    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 include NOT EXISTS somehow, 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 $range in my code is basically all orders that are 11 – 8 days old. And I only want the orders that do not have the meta x_days_follow_up_email, or when that meta is set but not to yes.

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello @dekadinious ,

    Thanks for reaching out!

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    If you need a paid solution, you can consider taking help from one of our partner’s website.

    Thank you 🙂

    Hi there,

    We’ve not heard back from you in a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Thank you 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WC_Order_Query where meta data does not exist or is not equal to’ is closed to new replies.