Skip to content

Commit

Permalink
Improve performance of WP_Meta_Query when doing OR queries on meta ke…
Browse files Browse the repository at this point in the history
…ys. Props joehoyle, SergeyBiryukov. fixes #19729

git-svn-id: http://core.svn.wordpress.org/trunk@22103 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
Ryan Boren committed Oct 3, 2012
1 parent ca42758 commit 778ba1c
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion wp-includes/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,30 @@ function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
$join = array();
$where = array();

foreach ( $this->queries as $k => $q ) {
$key_only_queries = array();
$queries = array();

// Split out the meta_key only queries (we can only do this for OR)
if ( 'OR' == $this->relation ) {
foreach ( $this->queries as $k => $q ) {
if ( ! isset( $q['value'] ) && ! empty( $q['key'] ) )
$key_only_queries[$k] = $q;
else
$queries[$k] = $q;
}
} else {
$queries = $this->queries;
}

// Specify all the meta_key only queries in one go
if ( $key_only_queries ) {
$join[] = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column";

foreach ( $key_only_queries as $key => $q )
$where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) );
}

foreach ( $queries as $k => $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';

Expand Down

0 comments on commit 778ba1c

Please sign in to comment.