WordPress.org

Make WordPress Core

Changeset 17674


Ignore:
Timestamp:
04/21/2011 06:13:03 PM (9 years ago)
Author:
ryan
Message:

Allow querying empty meta values. Props scribu. fixes #15292

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r17530 r17674  
    388388    foreach ( $meta_query as $q ) {
    389389        $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
    390         $meta_value = isset( $q['value'] ) ? $q['value'] : '';
    391390        $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
    392391        $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
     
    414413            $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
    415414
     415        if ( !isset( $q['value'] ) )
     416            continue;
     417        $meta_value = $q['value'];
     418
    416419        if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
    417420            if ( ! is_array( $meta_value ) )
    418421                $meta_value = preg_split( '/[,\s]+/', $meta_value );
     422
     423            if ( empty( $meta_value ) )
     424                continue;
    419425        } else {
    420426            $meta_value = trim( $meta_value );
    421427        }
    422 
    423         if ( empty( $meta_value ) )
    424             continue;
    425428
    426429        if ( 'IN' == substr( $meta_compare, -2) ) {
     
    436439        }
    437440
    438         // @todo Temporary hack to support empty values. Do not use outside of core.
    439         if ( '_wp_zero_value' == $meta_value )
    440             $meta_value = 0;
    441 
    442441        $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
    443442    }
     
    458457
    459458    // Simple query needs to be first for orderby=meta_value to work correctly
    460     foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) {
     459    foreach ( array( 'key', 'compare', 'type' ) as $key ) {
    461460        if ( !empty( $qv[ "meta_$key" ] ) )
    462461            $meta_query[0][ $key ] = $qv[ "meta_$key" ];
    463462    }
    464463
     464    // WP_Query sets 'meta_value' = '' by default
     465    if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
     466        $meta_query[0]['value'] = $qv[ 'meta_value' ];
     467
    465468    if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
    466469        $meta_query = array_merge( $meta_query, $qv['meta_query'] );
  • trunk/wp-includes/user.php

    r17634 r17674  
    497497        if ( 'authors' == $qv['who'] && $blog_id ) {
    498498            $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
    499             $qv['meta_value'] = '_wp_zero_value'; // Hack to pass '0'
     499            $qv['meta_value'] = 0;
    500500            $qv['meta_compare'] = '!=';
    501501            $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
Note: See TracChangeset for help on using the changeset viewer.