WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom field named 'rating' that holds decimals from 1.0 to 10. I am trying use wp_query to get me all the posts with a rating of 8.0 to 10. What I have so far almost works perfectly but it does not return posts with rating of 10.

<?php add_filter('get_meta_sql','cast_decimal_precision');
    function cast_decimal_precision( $array ) {
    $array['where'] = str_replace('DECIMAL','DECIMAL(1,1)',$array['where']);
    return $array;}
$args = array(
    'posts_per_page' => 10,
    'paged' => get_query_var( 'paged' ),
    'category_name' => ( 'movies' ),
    'meta_query' => array(
                    'key' => 'rating',
                    'value' => 8,
                    'compare' => '>'
    'meta_key' => 'rating',         
    'orderby' => 'meta_value',
    'order' => 'DESC'
$wp_query = null;
$wp_query = new WP_Query($args); 
share|improve this question
What happens if you set 'type' => 'NUMERIC' on your meta_query? – mor7ifer Mar 7 '12 at 18:04
When I did that none of the posts for ratings in 8 showed up. Only 9, 9.5 and 10. So 10 did show up but not in the right order and again left out all of the 8's. – Dustin J Mar 7 '12 at 18:33
Well yea, you used an exclusive comparator. Is 8 greater than 8? I think not. 8 is equal to 8...so you want your comparator to be >=. I'll go ahead and submit this as a solution. – mor7ifer Mar 7 '12 at 18:41
Oops. You are right. But the 10's are now showing up at the end. I have a order DESC so they should be at the top. Any way to fix that as well? – Dustin J Mar 7 '12 at 18:47
See the solution I just submitted, if that's still an issue post a comment on the solution. – mor7ifer Mar 7 '12 at 18:48
up vote 2 down vote accepted

The issue is that the default comparison for meta_query is a CHAR comparison, so the order that the numbers would be in by default would be 1, 10, 2, 3, 4, 5, 6, 7, 8, 9, a, etc. To resolve this you need to modify your meta_query as follows:

'meta_query' => array(
        'key'     => 'rating',
        'value'   => 8,
        'compare' => '>=',
        'type'    => 'NUMERIC'
share|improve this answer
The 10's are still showing up at the end of the list rather than at the top. – Dustin J Mar 7 '12 at 19:02
That's because meta_value is a CHAR ordering system. meta_value_num is a NUMERIC ordering system. Read the documentation for WP_Query before you ask for further clarification please. – mor7ifer Mar 7 '12 at 19:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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