Leave a Reply

2 comments

  1. You can filter generated SQL and add precision parameters that you need.

    Enable filters for get_posts() by adding following to query:

    'suppress_filters' => false,
    

    And:

    add_filter('posts_where','cast_decimal_precision');
    
    function cast_decimal_precision( $where ) {
    
        return str_replace('DECIMAL','DECIMAL(10,3)',$where);
    }
    

    Update

    With Jan’s suggestion:

    add_filter('get_meta_sql','cast_decimal_precision');
    
    function cast_decimal_precision( $array ) {
    
        $array['where'] = str_replace('DECIMAL','DECIMAL(10,3)',$array['where']);
    
        return $array;
    }
    
  2. As of 3.8 (see track) the precision can be added to the cast type like so:

    $posts = get_posts(array(
        'posts_per_page' => 100,
        'post_type' => 'place',
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'places_lat',
                'value' => array($lat_min, $lat_max),
                'compare' => 'BETWEEN',
                'type' => 'DECIMAL(10,3)',
            ),
            array(
                'key' => 'places_lng',
                'value' => array($lng_min, $lng_max),
                'compare' => 'BETWEEN',
                'type' => 'DECIMAL(10,3)',
            ),
        ),
    ));