Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress meta query not working with decimal type

Tags:

wordpress

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 );
?>
like image 817
Amin Avatar asked Oct 28 '25 02:10

Amin


1 Answers

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.

like image 145
Dog Dogma Avatar answered Oct 30 '25 17:10

Dog Dogma