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

scribu scribu at gmail.com
Mon Mar 29 20:42:48 UTC 2010


On Mon, Mar 29, 2010 at 6:00 PM, fscan <fscan at chilicode.com> wrote:

> 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']);
> }
>


Instead of modifying a core file, I would use the 'posts_where' filter.


-- 
http://scribu.net


More information about the wp-hackers mailing list