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?

  • I just ran SELECT * FROM wp_postmeta WHERE meta_key = 'clearing_width' AND meta_value BETWEEN 10 AND 1000; through the database and got the expected result. There must be something going on with the key?? – Chris Stage Mar 18 at 21:44
  • Well, it started working for seemingly no reason. Perhaps I had named something wrong somewhere. If I track it down I'll write about it. – Chris Stage Mar 18 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.