Get rid of redundant $this->meta_query. See #14645

git-svn-id: http://svn.automattic.com/wordpress/trunk@15766 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
scribu 2010-10-09 10:48:13 +00:00
parent 50dc04d43b
commit e157f923ed
3 changed files with 35 additions and 47 deletions

View File

@ -544,10 +544,37 @@ class WP {
*/ */
class WP_Object_Query { class WP_Object_Query {
/** /*
* List of metadata queries * Populates the $meta_query property
* *
* A query is an associative array: * @access protected
* @since 3.1.0
*
* @param array $qv The query variables
*/
function parse_meta_query( &$qv ) {
$meta_query = array();
// Simple query needs to be first for orderby=meta_value to work correctly
foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) {
if ( !empty( $qv[ "meta_$key" ] ) )
$meta_query[0][ $key ] = $qv[ "meta_$key" ];
}
if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
$meta_query = array_merge( $meta_query, $qv['meta_query'] );
}
$qv['meta_query'] = $meta_query;
}
/*
* Used internally to generate an SQL string for searching across multiple meta key = value pairs
*
* @access protected
* @since 3.1.0
*
* @param array $meta_query List of metadata queries. A single query is an associative array:
* - 'key' string The meta key * - 'key' string The meta key
* - 'value' string|array The meta value * - 'value' string|array The meta value
* - 'compare' (optional) string How to compare the key to the value. * - 'compare' (optional) string How to compare the key to the value.
@ -557,51 +584,13 @@ class WP_Object_Query {
* Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
* Default: 'CHAR' * Default: 'CHAR'
* *
* @since 3.1.0
* @access public
* @var array
*/
var $meta_query = array();
/*
* Populates the $meta_query property
*
* @access protected
* @since 3.1.0
*
* @param array $qv The query variables
*/
function parse_meta_query( $qv ) {
if ( ! empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
$this->meta_query = $qv['meta_query'];
}
$meta_query = array();
foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) {
if ( ! empty( $qv[ "meta_$key" ] ) )
$meta_query[ $key ] = $qv[ "meta_$key" ];
}
if ( ! empty( $meta_query ) ) {
array_unshift( $this->meta_query, $meta_query );
}
}
/*
* Used internally to generate an SQL string for searching across multiple meta key = value pairs
*
* @access protected
* @since 3.1.0
*
* @uses $this->meta_query
*
* @param string $primary_table * @param string $primary_table
* @param string $primary_id_column * @param string $primary_id_column
* @param string $meta_table * @param string $meta_table
* @param string $meta_id_column * @param string $meta_id_column
* @return array( $join_sql, $where_sql ) * @return array( $join_sql, $where_sql )
*/ */
function get_meta_sql( $primary_table, $primary_id_column, $meta_table, $meta_id_column ) { function get_meta_sql( $meta_query, $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {
global $wpdb; global $wpdb;
$clauses = array(); $clauses = array();
@ -609,7 +598,7 @@ class WP_Object_Query {
$join = ''; $join = '';
$where = ''; $where = '';
$i = 0; $i = 0;
foreach ( $this->meta_query as $q ) { foreach ( $meta_query as $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_value = isset( $q['value'] ) ? $q['value'] : ''; $meta_value = isset( $q['value'] ) ? $q['value'] : '';
$meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '='; $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';

View File

@ -1081,7 +1081,6 @@ class WP_Query extends WP_Object_Query {
unset($this->posts); unset($this->posts);
unset($this->query); unset($this->query);
$this->query_vars = array(); $this->query_vars = array();
$this->meta_query = array();
unset($this->queried_object); unset($this->queried_object);
unset($this->queried_object_id); unset($this->queried_object_id);
$this->post_count = 0; $this->post_count = 0;
@ -2114,7 +2113,7 @@ class WP_Query extends WP_Object_Query {
$where .= ')'; $where .= ')';
} }
list( $meta_join, $meta_where ) = $this->get_meta_sql( $wpdb->posts, 'ID', $wpdb->postmeta, 'post_id' ); list( $meta_join, $meta_where ) = $this->get_meta_sql( $q['meta_query'], $wpdb->posts, 'ID', $wpdb->postmeta, 'post_id' );
$join .= $meta_join; $join .= $meta_join;
$where .= $meta_where; $where .= $meta_where;

View File

@ -458,10 +458,10 @@ class WP_User_Query extends WP_Object_Query {
$cap_meta_query['compare'] = 'like'; $cap_meta_query['compare'] = 'like';
} }
$this->meta_query[] = $cap_meta_query; $qv['meta_query'][] = $cap_meta_query;
} }
list( $meta_join, $meta_where ) = $this->get_meta_sql( $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' ); list( $meta_join, $meta_where ) = $this->get_meta_sql( $qv['meta_query'], $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' );
$this->query_from .= $meta_join; $this->query_from .= $meta_join;
$this->query_where .= $meta_where; $this->query_where .= $meta_where;