I’m trying to use meta_query parmater to get posts with values between two values, the problem is it doesn’t work for all the numbers.
This is work:
$array[] = array(
'key' => 'd_weight',
'value' => array(0,0.5),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
But for some reason this doesn’t work:
$array[] = array(
'key' => 'd_weight',
'value' => array(0.1,0.5),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
The problem only occurs with numbers less than 1, on bigger number there is no problem…
Thanks for the help.
For decimal numbers, you should use ‘DECIMAL’ in place of ‘NUMERIC’
Mridul’s answer is partially correct, but there’s more to it in the OP’s case, specifically when dealing with the BETWEEN comparison. After changing from type=NUMERIC to type=DECIMAL, you also need special handling to filter the way WP typecasts to the DECIMAL datatype (otherwise it will use the default precision.) Very rough example below: