Ticket #17264: 17264.3.diff
File 17264.3.diff, 3.4 KB (added by , 13 years ago) |
---|
-
wp-includes/meta.php
476 476 477 477 $meta_id_column = esc_sql( $type . '_id' ); 478 478 479 $join = '';479 $join = array(); 480 480 $where = array(); 481 $i = 0; 481 482 482 foreach ( $this->queries as $k => $q ) { 483 483 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; 484 484 $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '='; 485 485 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR'; 486 486 487 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) 487 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 488 488 $meta_compare = '='; 489 } 489 490 490 if ( 'NUMERIC' == $meta_type ) 491 if ( 'NUMERIC' == $meta_type ) { 491 492 $meta_type = 'SIGNED'; 492 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )493 } elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) ) { 493 494 $meta_type = 'CHAR'; 495 } 494 496 495 if ( empty( $meta_key ) && empty( $meta_value ) ) 496 continue; 497 497 $i = count( $join ); 498 498 $alias = $i ? 'mt' . $i : $meta_table; 499 499 500 $join .= "\nINNER JOIN $meta_table"; 501 $join .= $i ? " AS $alias" : ''; 502 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; 500 // Set JOIN 501 $join[$i] = "INNER JOIN $meta_table"; 502 $join[$i] .= $i ? " AS $alias" : ''; 503 $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; 503 504 504 $i++; 505 505 $where[$k] = ''; 506 506 if ( !empty( $meta_key ) ) 507 507 $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key ); 508 508 509 if ( !isset( $q['value'] ) ) 509 if ( !isset( $q['value'] ) ) { 510 if ( empty( $where[$k] ) ) { 511 unset( $join[$i] ); 512 } 510 513 continue; 514 } 511 515 $meta_value = $q['value']; 512 516 513 517 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 514 518 if ( ! is_array( $meta_value ) ) 515 519 $meta_value = preg_split( '/[,\s]+/', $meta_value ); 516 520 517 if ( empty( $meta_value ) ) 521 if ( empty( $meta_value ) ) { 522 unset( $join[$i] ); 518 523 continue; 524 } 519 525 } else { 520 526 $meta_value = trim( $meta_value ); 521 527 } … … 531 537 } else { 532 538 $meta_compare_string = '%s'; 533 539 } 540 541 if ( $where[$k] ) 542 $where[$k] .= ' AND '; 534 543 535 $where[$k] = ' (' . $where[$k] . $wpdb->prepare( " ANDCAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value );544 $where[$k] = ' (' . $where[$k] . $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value ); 536 545 } 537 $where = ' AND (' . implode( " {$this->relation} ", $where ) . ' )';538 546 547 $where = array_filter( $where ); 548 549 if ( empty( $where ) ) { 550 $where = ''; 551 } else { 552 $where = ' AND (' . implode( "\n{$this->relation} ", $where ) . ' )'; 553 } 554 555 $join = implode( "\n", $join ); 556 if ( !empty( $join ) ) 557 $join = ' ' . $join; 558 539 559 return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) ); 540 560 } 541 542 561 } 543 562 544 563 /**