Custom WP_Query – meta_query ignored

I would like to display some “artworks” with specifics meta key values. Each artworks has a custom field : “votes_average” and another one “votes_count”. This is my custom query :

$args = array(
    'post_type' => 'artworks',
    'post_status' => 'publish',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'meta_query' => array(
        'relation' => 'AND',
        array(
           'key' => 'votes_average',
           'value' => '6',
           'compare' => '>=',
           'type'    => 'DECIMAL',
        ),
        array(
           'key' => 'votes_count',
           'value' => '1',
           'compare' => '>',
           'type'    => 'NUMERIC',
        ),
    ),
    'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => $term
        )
    )
);

I don’t understand why this query display an artwork with an average of 5,8. Did I make a mistake ?

Related posts

Leave a Reply

1 comment

  1. The problem is that MySQL rounded 5.8 to 6 and used in the query. Appropriate query portion looks like

    (wp_postmeta.meta_key = 'votes_average' AND CAST(wp_postmeta.meta_value AS DECIMAL) >= '6')
    

    So you can replace DECIMAL with NUMERIC to solve this issue.

        array(
           'key' => 'votes_average',
           'value' => '6',
           'compare' => '>=',
           'type'    => 'NUMERIC',
        ),