1

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 ?

1
  • ¿What's the correct average for your example? Jan 1, 2013 at 13:57

1 Answer 1

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',
    ),
1
  • 1
    Thank you for your explantion. The artwork with 5,8 of average doesn't appear anymore. Thank you and happy new year !
    – Adrien G
    Jan 1, 2013 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.