How to use Meta Value Compare in WP_Query For Max and Min numbers

This looks right, I have very similar code that works as expected:

function price_query_test() {

    update_post_meta( 7, '_price', 10 );
    update_post_meta( 12, '_price', 20 );
    update_post_meta( 18, '_price', 30 );
    update_post_meta( 72, '_price', 40 );

    $test = new WP_Query( array(
        'post_type'     => 'page',
        'meta_query'    => array( array(
            'key'       => '_price',
            'value'     => array( '10', '25' ),
            'compare'   => 'BETWEEN',
        ) ),
        'fields' => 'ids',
    ) );

    printf( '<pre>%s</pre>', print_r( $test->posts, 1 ) );
    die();

}
add_action( 'init', 'price_query_test' );

The above gives me an array:

Array
(
    [0] => 12
    [1] => 7
)

If we wanted to we could also add in our meta_query a type:

'meta_query'    => array( array(
    'key'       => '_price',
    'value'     => array( '10', '25' ),
    'compare'   => 'BETWEEN',
    'type'      => 'NUMERIC',
) )

But it seems to work as expected without the meta_type.

If you’re using WooCommerce, do note that their metavalue is _price and not price.

tech