<!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" />
<title>[31312] trunk: Improve support for ordering `WP_Query` results by postmeta.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/31312">31312</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/31312","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-01-31 15:47:51 +0000 (Sat, 31 Jan 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Improve support for ordering `WP_Query` results by postmeta.

`WP_Meta_Query` clauses now support a 'name' parameter. When building a
`WP_Query` object, the value of 'orderby' can reference this 'name', so that
it's possible to order by any clause in a meta_query, not just the first one
(as when using 'orderby=meta_value'). This improvement also makes it possible
to order by multiple meta query clauses (or by any other eligible field plus
a meta query clause), using the array syntax for 'orderby' introduced in <a href="https://core.trac.wordpress.org/changeset/29027">[29027]</a>.

Props Funkatronic, boonebgorges.
Fixes <a href="https://core.trac.wordpress.org/ticket/31045">#31045</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmetaphp">trunk/src/wp-includes/meta.php</a></li>
<li><a href="#trunksrcwpincludesqueryphp">trunk/src/wp-includes/query.php</a></li>
<li><a href="#trunktestsphpunittestsquerymetaQueryphp">trunk/tests/phpunit/tests/query/metaQuery.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/meta.php    2015-01-31 02:11:46 UTC (rev 31311)
+++ trunk/src/wp-includes/meta.php      2015-01-31 15:47:51 UTC (rev 31312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -934,9 +934,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected $table_aliases = array();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * A flat list of clauses, keyed by clause 'name'.
+        *
+        * @since 4.2.0
+        * @var array
+        */
+       protected $clauses = array();
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Constructor.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.2.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 4.2.0 Introduced the `$name` parameter, for improved `$orderby` support in the parent query.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @access public
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $meta_query {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -957,6 +967,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *                               comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
</span><span class="cx" style="display: block; padding: 0 10px">         *                               'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
</span><span class="cx" style="display: block; padding: 0 10px">         *                               Default is 'CHAR'.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *         @type string $name    Optional. A unique identifier for the clause. If provided, `$name` can be
+        *                               referenced in the `$orderby` parameter of the parent query.
</ins><span class="cx" style="display: block; padding: 0 10px">          *     }
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1374,6 +1386,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Save the alias to this clause, for future siblings to find.
</span><span class="cx" style="display: block; padding: 0 10px">                $clause['alias'] = $alias;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Determine the data type.
+               $_meta_type = isset( $clause['type'] ) ? $clause['type'] : '';
+               $meta_type  = $this->get_cast_for_type( $_meta_type );
+               $clause['cast'] = $meta_type;
+
+               // Store the clause in our flat array.
+               $clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias'];
+               $this->clauses[ $clause_name ] =& $clause;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Next, build the WHERE clause.
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // meta_key.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1388,7 +1409,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // meta_value.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( array_key_exists( 'value', $clause ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $meta_value = $clause['value'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $meta_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! is_array( $meta_value ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1451,6 +1471,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Get a flattened list of sanitized meta clauses, indexed by clause 'name'.
+        *
+        * This array should be used for clause lookup, as when the table alias and CAST type must be determined for
+        * a value of 'orderby' corresponding to a meta clause.
+        *
+        * @since 4.2.0
+        * @access public
+        *
+        * @return array
+        */
+       public function get_clauses() {
+               return $this->clauses;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Identify an existing table alias that is compatible with the current
</span><span class="cx" style="display: block; padding: 0 10px">         * query clause.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre></div>
<a id="trunksrcwpincludesqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/query.php   2015-01-31 02:11:46 UTC (rev 31311)
+++ trunk/src/wp-includes/query.php     2015-01-31 15:47:51 UTC (rev 31312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2233,8 +2233,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $primary_meta_key = '';
</span><span class="cx" style="display: block; padding: 0 10px">                $primary_meta_query = false;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $this->meta_query->queries ) ) {
-                       $primary_meta_query = reset( $this->meta_query->queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $meta_clauses = $this->meta_query->get_clauses();
+               if ( ! empty( $meta_clauses ) ) {
+                       $primary_meta_query = reset( $meta_clauses );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! empty( $primary_meta_query['key'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $primary_meta_key = $primary_meta_query['key'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2243,6 +2244,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $allowed_keys[] = 'meta_value';
</span><span class="cx" style="display: block; padding: 0 10px">                        $allowed_keys[] = 'meta_value_num';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $allowed_keys   = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! in_array( $orderby, $allowed_keys ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2260,29 +2262,36 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'ID':
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'menu_order':
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'comment_count':
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $orderby = "$wpdb->posts.{$orderby}";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $orderby_clause = "$wpdb->posts.{$orderby}";
</ins><span class="cx" style="display: block; padding: 0 10px">                                 break;
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'rand':
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $orderby = 'RAND()';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $orderby_clause = 'RAND()';
</ins><span class="cx" style="display: block; padding: 0 10px">                                 break;
</span><span class="cx" style="display: block; padding: 0 10px">                        case $primary_meta_key:
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'meta_value':
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! empty( $primary_meta_query['type'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $sql_type = $this->meta_query->get_cast_for_type( $primary_meta_query['type'] );
-                                       $orderby = "CAST($wpdb->postmeta.meta_value AS {$sql_type})";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
</ins><span class="cx" style="display: block; padding: 0 10px">                                 } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $orderby = "$wpdb->postmeta.meta_value";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $orderby_clause = "{$primary_meta_query['alias']}.meta_value";
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px">                                break;
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'meta_value_num':
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $orderby = "$wpdb->postmeta.meta_value+0";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0";
</ins><span class="cx" style="display: block; padding: 0 10px">                                 break;
</span><span class="cx" style="display: block; padding: 0 10px">                        default:
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $orderby = "$wpdb->posts.post_" . $orderby;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( array_key_exists( $orderby, $meta_clauses ) ) {
+                                       // $orderby corresponds to a meta_query clause.
+                                       $meta_clause = $meta_clauses[ $orderby ];
+                                       $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
+                               } else {
+                                       // Default: order by post field.
+                                       $orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby );
+                               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 break;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return $orderby;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return $orderby_clause;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2813,6 +2822,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $where .= $search . $whichauthor . $whichmimetype;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( ! empty( $this->meta_query->queries ) ) {
+                       $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
+                       $join   .= $clauses['join'];
+                       $where  .= $clauses['where'];
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! isset( $q['order'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $q['order'] = $rand ? '' : 'DESC';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3030,12 +3045,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $where .= ')';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $this->meta_query->queries ) ) {
-                       $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
-                       $join .= $clauses['join'];
-                       $where .= $clauses['where'];
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 /*
</span><span class="cx" style="display: block; padding: 0 10px">                 * Apply filters on where and join prior to paging so that any
</span><span class="cx" style="display: block; padding: 0 10px">                 * manipulations to them are reflected in the paging by day queries.
</span></span></pre></div>
<a id="trunktestsphpunittestsquerymetaQueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/query/metaQuery.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/query/metaQuery.php     2015-01-31 02:11:46 UTC (rev 31311)
+++ trunk/tests/phpunit/tests/query/metaQuery.php       2015-01-31 15:47:51 UTC (rev 31312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1558,4 +1558,101 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $posts = wp_list_pluck( $posts, 'ID' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 31045
+        */
+       public function test_orderby_name() {
+               $posts = $this->factory->post->create_many( 3 );
+               add_post_meta( $posts[0], 'foo', 'aaa' );
+               add_post_meta( $posts[1], 'foo', 'zzz' );
+               add_post_meta( $posts[2], 'foo', 'jjj' );
+
+               $q = new WP_Query( array(
+                       'fields' => 'ids',
+                       'meta_query' => array(
+                               array(
+                                       'name' => 'foo_name',
+                                       'key' => 'foo',
+                                       'compare' => 'EXISTS',
+                               ),
+                       ),
+                       'orderby' => 'foo_name',
+                       'order' => 'DESC',
+               ) );
+
+               $this->assertEquals( array( $posts[1], $posts[2], $posts[0] ), $q->posts );
+       }
+
+       /**
+        * @ticket 31045
+        */
+       public function test_orderby_name_as_secondary_sort() {
+               $p1 = $this->factory->post->create( array(
+                       'post_date' => '2015-01-28 03:00:00',
+               ) );
+               $p2 = $this->factory->post->create( array(
+                       'post_date' => '2015-01-28 05:00:00',
+               ) );
+               $p3 = $this->factory->post->create( array(
+                       'post_date' => '2015-01-28 03:00:00',
+               ) );
+
+               add_post_meta( $p1, 'foo', 'jjj' );
+               add_post_meta( $p2, 'foo', 'zzz' );
+               add_post_meta( $p3, 'foo', 'aaa' );
+
+               $q = new WP_Query( array(
+                       'fields' => 'ids',
+                       'meta_query' => array(
+                               array(
+                                       'name' => 'foo_name',
+                                       'key' => 'foo',
+                                       'compare' => 'EXISTS',
+                               ),
+                       ),
+                       'orderby' => array(
+                               'post_date' => 'asc',
+                               'foo_name' => 'asc',
+                       ),
+               ) );
+
+               $this->assertEquals( array( $p3, $p1, $p2 ), $q->posts );
+       }
+
+       /**
+        * @ticket 31045
+        */
+       public function test_orderby_more_than_one_name() {
+               $posts = $this->factory->post->create_many( 3 );
+
+               add_post_meta( $posts[0], 'foo', 'jjj' );
+               add_post_meta( $posts[1], 'foo', 'zzz' );
+               add_post_meta( $posts[2], 'foo', 'jjj' );
+               add_post_meta( $posts[0], 'bar', 'aaa' );
+               add_post_meta( $posts[1], 'bar', 'ccc' );
+               add_post_meta( $posts[2], 'bar', 'bbb' );
+
+               $q = new WP_Query( array(
+                       'fields' => 'ids',
+                       'meta_query' => array(
+                               array(
+                                       'name' => 'foo_name',
+                                       'key' => 'foo',
+                                       'compare' => 'EXISTS',
+                               ),
+                               array(
+                                       'name' => 'bar_name',
+                                       'key' => 'bar',
+                                       'compare' => 'EXISTS',
+                               ),
+                       ),
+                       'orderby' => array(
+                               'foo_name' => 'asc',
+                               'bar_name' => 'desc',
+                       ),
+               ) );
+
+               $this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $q->posts );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>