Changeset 16266
- Timestamp:
- 11/09/2010 11:22:13 PM (14 years ago)
- Location:
- trunk/wp-includes
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/class-wp-object-query.php
r16170 r16266 70 70 71 71 $qv['meta_query'] = $meta_query; 72 }73 74 /*75 * Used internally to generate an SQL string for searching across multiple meta key = value pairs76 *77 * @access protected78 * @since 3.1.079 *80 * @param array $meta_query List of metadata queries. A single query is an associative array:81 * - 'key' string The meta key82 * - 'value' string|array The meta value83 * - 'compare' (optional) string How to compare the key to the value.84 * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.85 * Default: '='86 * - 'type' string (optional) The type of the value.87 * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.88 * Default: 'CHAR'89 *90 * @param string $meta_type91 * @param string $primary_table92 * @param string $primary_id_column93 * @return array( $join_sql, $where_sql )94 */95 function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_column ) {96 global $wpdb;97 98 if ( ! $meta_table = _get_meta_table( $meta_type ) )99 return false;100 101 $meta_id_column = esc_sql( $meta_type . '_id' );102 103 $clauses = array();104 105 $join = '';106 $where = '';107 $i = 0;108 foreach ( $meta_query as $q ) {109 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';110 $meta_value = isset( $q['value'] ) ? $q['value'] : '';111 $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';112 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';113 114 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )115 $meta_compare = '=';116 117 if ( 'NUMERIC' == $meta_type )118 $meta_type = 'SIGNED';119 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )120 $meta_type = 'CHAR';121 122 if ( empty( $meta_key ) && empty( $meta_value ) )123 continue;124 125 $alias = $i ? 'mt' . $i : $meta_table;126 127 $join .= "\nINNER JOIN $meta_table";128 $join .= $i ? " AS $alias" : '';129 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";130 131 $i++;132 133 if ( !empty( $meta_key ) )134 $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );135 136 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {137 if ( ! is_array( $meta_value ) )138 $meta_value = preg_split( '/[,\s]+/', $meta_value );139 } else {140 $meta_value = trim( $meta_value );141 }142 143 if ( empty( $meta_value ) )144 continue;145 146 if ( 'IN' == substr( $meta_compare, -2) ) {147 $meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 );148 $meta_compare_string = "($meta_field_types)";149 unset( $meta_field_types );150 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {151 $meta_value = array_slice( $meta_value, 0, 2 );152 $meta_compare_string = '%s AND %s';153 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {154 $meta_value = '%' . like_escape( $meta_value ) . '%';155 $meta_compare_string = '%s';156 } else {157 $meta_compare_string = '%s';158 }159 $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );160 161 unset( $meta_compare_string );162 }163 164 return apply_filters( 'get_meta_sql', compact( 'join', 'where' ), $meta_query, $meta_type, $primary_table, $primary_id_column );165 72 } 166 73 -
trunk/wp-includes/meta.php
r16017 r16266 352 352 } 353 353 354 /* 355 * Given a meta query, generates SQL clauses to be appended to a main query 356 * 357 * @since 3.1.0 358 * 359 * @param array $meta_query List of metadata queries. A single query is an associative array: 360 * - 'key' string The meta key 361 * - 'value' string|array The meta value 362 * - 'compare' (optional) string How to compare the key to the value. 363 * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. 364 * Default: '=' 365 * - 'type' string (optional) The type of the value. 366 * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. 367 * Default: 'CHAR' 368 * 369 * @param string $meta_type 370 * @param string $primary_table 371 * @param string $primary_id_column 372 * @return array( 'join' => $join_sql, 'where' => $where_sql ) 373 */ 374 function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_column ) { 375 global $wpdb; 376 377 if ( ! $meta_table = _get_meta_table( $meta_type ) ) 378 return false; 379 380 $meta_id_column = esc_sql( $meta_type . '_id' ); 381 382 $clauses = array(); 383 384 $join = ''; 385 $where = ''; 386 $i = 0; 387 foreach ( $meta_query as $q ) { 388 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; 389 $meta_value = isset( $q['value'] ) ? $q['value'] : ''; 390 $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '='; 391 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR'; 392 393 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) 394 $meta_compare = '='; 395 396 if ( 'NUMERIC' == $meta_type ) 397 $meta_type = 'SIGNED'; 398 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) ) 399 $meta_type = 'CHAR'; 400 401 if ( empty( $meta_key ) && empty( $meta_value ) ) 402 continue; 403 404 $alias = $i ? 'mt' . $i : $meta_table; 405 406 $join .= "\nINNER JOIN $meta_table"; 407 $join .= $i ? " AS $alias" : ''; 408 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; 409 410 $i++; 411 412 if ( !empty( $meta_key ) ) 413 $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key ); 414 415 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 416 if ( ! is_array( $meta_value ) ) 417 $meta_value = preg_split( '/[,\s]+/', $meta_value ); 418 } else { 419 $meta_value = trim( $meta_value ); 420 } 421 422 if ( empty( $meta_value ) ) 423 continue; 424 425 if ( 'IN' == substr( $meta_compare, -2) ) { 426 $meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 ); 427 $meta_compare_string = "($meta_field_types)"; 428 unset( $meta_field_types ); 429 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) { 430 $meta_value = array_slice( $meta_value, 0, 2 ); 431 $meta_compare_string = '%s AND %s'; 432 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) { 433 $meta_value = '%' . like_escape( $meta_value ) . '%'; 434 $meta_compare_string = '%s'; 435 } else { 436 $meta_compare_string = '%s'; 437 } 438 $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value ); 439 440 unset( $meta_compare_string ); 441 } 442 443 return apply_filters( 'get_meta_sql', compact( 'join', 'where' ), $meta_query, $meta_type, $primary_table, $primary_id_column ); 444 } 445 354 446 /** 355 447 * Retrieve the name of the metadata table for the specified object type. -
trunk/wp-includes/query.php
r16259 r16266 2148 2148 2149 2149 if ( !empty( $q['meta_query'] ) ) { 2150 $clauses = $this->get_meta_sql( $q['meta_query'], 'post', $wpdb->posts, 'ID' );2150 $clauses = get_meta_sql( $q['meta_query'], 'post', $wpdb->posts, 'ID' ); 2151 2151 $join .= $clauses['join']; 2152 2152 $where .= $clauses['where']; -
trunk/wp-includes/user.php
r16262 r16266 482 482 483 483 if ( !empty( $qv['meta_query'] ) ) { 484 $clauses = $this->get_meta_sql( $qv['meta_query'], 'user', $wpdb->users, 'ID' );484 $clauses = get_meta_sql( $qv['meta_query'], 'user', $wpdb->users, 'ID' ); 485 485 $this->query_from .= $clauses['join']; 486 486 $this->query_where .= $clauses['where'];
Note: See TracChangeset
for help on using the changeset viewer.