[wp-hackers] Please Help: How to compare 2 float values in meta_value column?

fscan fscan at chilicode.com
Mon Mar 29 15:00:11 UTC 2010


babyboy808 <hi <at> eirestudio.net> writes:

> 
> 
> Hi Guys,
> 
> I have about 100 posts in wordpress, all with a meta_key of price and a
> value. How would I go about searching through all posts with a value between
> 23.00 and 41.00 for example?
> 
> I know that 'meta_compare ' treats 'meta_value' as a string, so I can't use
> this to solve my problem.
> 
> Anybody have any clues?
> 
> Thanks


i just hacked a little bit around ind wp-includes/query.php (2.8.4 ubuntu) to
support numeric compares. introduced a new query variable: meta_compare_num.
only tested with mysql backend. you could add an between operator like this.

line 2149: replace the whole if block with:

if ( ! empty($q['meta_value']) ) {
    if ( ! isset($q['meta_compare']) || empty($q['meta_compare']) || !
in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
        if ( ! isset($q['meta_compare_num']) || empty($q['meta_compare_num']) ||
! in_array($q['meta_compare_num'], array('=', '!=', '>', '>=', '<', '<=')) )
            $q['meta_compare'] = '=';
    if( isset($q['meta_compare']) )
        $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value
{$q['meta_compare']} %s ", $q['meta_value']);
    else
        $where .= $wpdb->prepare("AND CAST($wpdb->postmeta.meta_value AS
DECIMAL) {$q['meta_compare_num']} CAST(%s AS DECIMAL) ", $q['meta_value']);
}





More information about the wp-hackers mailing list