<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[15581] trunk/wp-includes: Use _wp_meta_sql() in WP_Query.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15581">15581</a></dd>
<dt>Author</dt> <dd>scribu</dd>
<dt>Date</dt> <dd>2010-09-06 11:28:59 +0000 (Mon, 06 Sep 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Use _wp_meta_sql() in WP_Query. See <a href="http://trac.wordpress.org/ticket/14572">#14572</a>. See <a href="http://trac.wordpress.org/ticket/14645">#14645</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.php</a></li>
<li><a href="#trunkwpincludesqueryphp">trunk/wp-includes/query.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (15580 => 15581)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2010-09-06 11:04:50 UTC (rev 15580)
+++ trunk/wp-includes/functions.php        2010-09-06 11:28:59 UTC (rev 15581)
</span><span class="lines">@@ -4254,6 +4254,9 @@
</span><span class="cx">
</span><span class="cx">         $clauses = array();
</span><span class="cx">
</span><ins>+        $join = '';
+        $where = '';
+        $i = 0;
</ins><span class="cx">         foreach ( $queries as $q ) {
</span><span class="cx">                 $meta_key = isset( $q['meta_key'] ) ? trim( $q['meta_key'] ) : '';
</span><span class="cx">                 $meta_value = isset( $q['meta_value'] ) ? trim( $q['meta_value'] ) : '';
</span><span class="lines">@@ -4262,37 +4265,28 @@
</span><span class="cx">                 if ( !in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'like' ) ) )
</span><span class="cx">                         $meta_compare = '=';
</span><span class="cx">
</span><del>-                if ( empty( $meta_key ) )
</del><ins>+                if ( empty( $meta_key ) && empty( $meta_value ) )
</ins><span class="cx">                         continue;
</span><span class="cx">
</span><del>-                if ( empty( $meta_value ) ) {
-                        $clauses[ $meta_key ] = "";
-                } elseif ( 'like' == $meta_compare ) {
-                        $clauses[ $meta_key ] = $wpdb->prepare( "LIKE %s", '%' . like_escape( $meta_value ) . '%' );
-                } else {
-                        $clauses[ $meta_key ] = $wpdb->prepare( "$meta_compare %s", $meta_value );
-                }
-        }
-
-        if ( empty( $clauses ) )
-                return array('', '');
-
-        $join = '';
-        $where = '';
-
-        $i = 0;
-        foreach ( $clauses as $meta_key => $value_query ) {
</del><span class="cx">                 $alias = $i ? 'mt' . $i : $meta_table;
</span><span class="cx">
</span><span class="cx">                 $join .= "\nINNER JOIN $meta_table";
</span><span class="cx">                 $join .= $i ? " AS $alias" : '';
</span><span class="cx">                 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
</span><span class="cx">
</span><del>-                $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
-                if ( !empty( $value_query ) )
-                        $where .= " AND $alias.meta_value $value_query";
-                        
</del><span class="cx">                 $i++;
</span><ins>+
+                if ( !empty( $meta_key ) )
+                        $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
+
+                if ( empty( $meta_value ) )
+                        continue;
+
+                if ( 'like' == $meta_compare ) {
+                        $where .= $wpdb->prepare( " AND $alias.meta_value LIKE %s", '%' . like_escape( $meta_value ) . '%' );
+                } else {
+                        $where .= $wpdb->prepare( " AND $alias.meta_value $meta_compare %s", $meta_value );
+                }
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         return array( $join, $where );
</span></span></pre></div>
<a id="trunkwpincludesqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/query.php (15580 => 15581)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/query.php        2010-09-06 11:04:50 UTC (rev 15580)
+++ trunk/wp-includes/query.php        2010-09-06 11:28:59 UTC (rev 15581)
</span><span class="lines">@@ -2211,17 +2211,11 @@
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 // postmeta queries
</span><del>-                if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
-                        $join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
-                if ( ! empty($q['meta_key']) )
-                        $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
-                if ( ! empty($q['meta_value']) ) {
-                        if ( empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
-                                $q['meta_compare'] = '=';
</del><ins>+                $meta_query = wp_array_slice_assoc( $q, array( 'meta_key', 'meta_value', 'meta_compare' ) );
+                list( $meta_join, $meta_where ) = _wp_meta_sql( array( $meta_query ), $wpdb->posts, 'ID', $wpdb->postmeta, 'post_id' );
+                $join .= $meta_join;
+                $where .= $meta_where;
</ins><span class="cx">
</span><del>-                        $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
-                }
-
</del><span class="cx">                 // Apply filters on where and join prior to paging so that any
</span><span class="cx">                 // manipulations to them are reflected in the paging by day queries.
</span><span class="cx">                 if ( !$q['suppress_filters'] ) {
</span></span></pre>
</div>
</div>
</body>
</html>