Make WordPress Core


Ignore:
Timestamp:
10/04/2010 09:37:08 PM (13 years ago)
Author:
scribu
Message:

Enhance get_meta_sql. Props aaroncampbell. Fixes #9124

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/classes.php

    r15723 r15724  
    590590        foreach ( $this->meta_query as $q ) {
    591591            $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
    592             $meta_value = isset( $q['value'] ) ? trim( $q['value'] ) : '';
    593             $meta_compare = isset( $q['compare'] ) ? $q['compare'] : '=';
    594 
    595             if ( !in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'like' ) ) )
     592            $meta_value = isset( $q['value'] ) ? $q['value'] : '';
     593            $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
     594            $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
     595
     596            if ( !in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'IN', 'BETWEEN' ) ) )
    596597                $meta_compare = '=';
     598
     599            if ( 'STRING' == $meta_type )
     600                $meta_type = 'CHAR';
     601
     602            if ( 'NUMERIC' == $meta_type )
     603                $meta_type = 'SIGNED';
     604
     605            if ( !in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
     606                $meta_type = 'CHAR';
    597607
    598608            if ( empty( $meta_key ) && empty( $meta_value ) )
     
    610620                $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
    611621
     622            if ( in_array( $meta_compare, array( 'IN', 'BETWEEN' ) ) ) {
     623                if ( !is_array( $meta_value ) )
     624                    $meta_value = preg_split( '/[,\s]+/', $meta_value );
     625            } else {
     626                $meta_value = trim( $meta_value );
     627            }
     628
    612629            if ( empty( $meta_value ) )
    613630                continue;
    614631
    615             if ( 'like' == $meta_compare ) {
    616                 $where .= $wpdb->prepare( " AND $alias.meta_value LIKE %s", '%' . like_escape( $meta_value ) . '%' );
     632            if ( 'IN' == $meta_compare ) {
     633                $meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 );
     634                $meta_compare_string = "($meta_field_types)";
     635                unset( $meta_field_types );
     636            } elseif ( 'BETWEEN' == $meta_compare ) {
     637                $meta_value = array_slice( $meta_value, 0, 2 );
     638                $meta_compare_string = '%s AND %s';
     639            } elseif ( 'LIKE' == $meta_compare ) {
     640                $meta_value = '%' . like_escape( $meta_value ) . '%';
     641                $meta_compare_string = '%s';
    617642            } else {
    618                 $where .= $wpdb->prepare( " AND $alias.meta_value $meta_compare %s", $meta_value );
     643                $meta_compare_string = '%s';
    619644            }
     645            $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
     646            unset($meta_compare_string);
    620647        }
    621648
Note: See TracChangeset for help on using the changeset viewer.