I'm trying to write a wordpress custom query ( WP_Query ) which filters posts by rating limit.
This rating is a number between 0 and 10 and it maybe floating numbers as well ( 6.8 for example ) , i tried this code but it doesn't work :(
<?php
$ratings = array( 4, 7 ); // this is an example , ratings are dynamic
$args = array(
    'post_type'  => 'product',
    'showposts'  => -1,
    'meta_query' => array(
        array(
            'key'     => 'aps-product-rating-total', // floating number
            'value'   => $ratings,
            'type'    => 'DECIMAL',
            'compare' => 'BETWEEN'
        )
    );
);
$filter_result = new WP_Query( $args );
?>
The answer is simple - remove the 'type' attribute and the search will respect your decimals. I learned this by outputting the query as suggested by m.cichacz. You can see that there is typecasting on the fields when either NUMERIC or DECIMAL is specified, but for some reason this is causes the query to ignore anything after the decimal place. Remove the typecasting and it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With