custom field - Why does my numeric meta query work only on one meta key and not the other?

admin2025-06-02  1

I have a meta query I'm building as part of a woocommerce product filter. We have two meta queries that can run together or separately depending on what filtering is selected. The intention is to get them both working together but I've disabled the _price filter for now.

Here is the query being built before filters:

$page = 1; // Default page to start on — can be overwritten.

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 12,
    'paged'  => $page,
    'orderby' => 'Date',
    'order'   => 'ASC',

    'tax_query' => array(),
    'meta_query' => array()
);

Here is when I run a price range filter, which produces the expected results: A range of products prices between 2,000 and 5,000.

$price_range = array(
    'key' => '_price',
    'value' => array(2000,5000),
    'compare' => 'BETWEEN',
    'type' => 'NUMERIC'
);
array_push( $args['meta_query'], $price_range );

Now, if I run this meta query, with or without the above price range filter added to the meta_query array I get no results.

$clearingWidth = array(
    'key' => 'clearing_width',
    'value' => array( 10,1000 ),
    'compare' => 'BETWEEN',
    'type' => 'NUMERIC'
);

array_push( $args['meta_query'], $clearingWidth );

I have my databased linked up to my IDE and I have queried for the meta_key clearing_width which I do have two results for. One with a value of 60, and another with 42. Should I not see them as results? For some reason the price key works but not clearing width.

Any ideas?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748794697a313776.html

最新回复(0)