Meta Query - Compare two meta values in one query

Viewed 339

I was looking on another question , and thought of changing the original query to compare two meta_key values.

This question deals with finding WC coupons that reached their usage limit. There are two values stored as post meta data: usage_limit and usage_count.

I want to find all the coupons that the usage_count is equal or greater than the usage_limit. Something like:

$args = array(
    'posts_per_page' => -1,
    'post_type'      => 'shop_coupon',
    'post_status'    => 'publish',
    'meta_query'     => array(
        array(
            'key'     => 'usage_count',
            'value'   => [meta_key='usage_limit'] value here,
            'compare' => '>='
        )
    )
);

I didn't find any solution for this type of query, but thought there might be an elegant way.

0 Answers
Related