<!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>[29027] trunk: Allow an `array()` to be passed as the value for `orderby` to `WP_Query`.</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">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/29027">29027</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2014-07-08 17:15:53 +0000 (Tue, 08 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Allow an `array()` to be passed as the value for `orderby` to `WP_Query`. Allows for an independent `order` value for each key.

Example: `'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' )`.

Adds docs and unit tests.

Props wonderboymusic, johnbillion, DrewAPicture, dd32, andy.
See <a href="http://core.trac.wordpress.org/ticket/17065">#17065</a>.</pre>

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

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/query.php (29026 => 29027)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/query.php  2014-07-08 17:14:20 UTC (rev 29026)
+++ trunk/src/wp-includes/query.php     2014-07-08 17:15:53 UTC (rev 29027)
</span><span class="lines">@@ -2200,6 +2200,97 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * If the passed orderby value is allowed, convert the alias to a
+        * properly-prefixed orderby value.
+        *
+        * @since 4.0.0
+        * @access protected
+        *
+        * @global wpdb $wpdb WordPress database access abstraction object.
+        *
+        * @param string $orderby Alias for the field to order by.
+        * @return string|bool Table-prefixed value to used in the ORDER clause. False otherwise.
+        */
+       protected function parse_orderby( $orderby ) {
+               global $wpdb;
+
+               // Used to filter values.
+               $allowed_keys = array(
+                       'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',
+                       'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified',
+                       'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand',
+               );
+
+               $meta_key = $this->get( 'meta_key' );
+               if ( ! empty( $meta_key ) ) {
+                       $allowed_keys[] = $meta_key;
+                       $allowed_keys[] = 'meta_value';
+                       $allowed_keys[] = 'meta_value_num';
+               }
+
+               if ( ! in_array( $orderby, $allowed_keys ) ) {
+                       return false;
+               }
+
+               switch ( $orderby ) {
+                       case 'post_name':
+                       case 'post_author':
+                       case 'post_date':
+                       case 'post_title':
+                       case 'post_modified':
+                       case 'post_parent':
+                       case 'post_type':
+                       case 'ID':
+                       case 'menu_order':
+                       case 'comment_count':
+                               $orderby = "$wpdb->posts.{$orderby}";
+                               break;
+                       case 'rand':
+                               $orderby = 'RAND()';
+                               break;
+                       case $meta_key:
+                       case 'meta_value':
+                               $type = $this->get( 'meta_type' );
+                               if ( ! empty( $type ) ) {
+                                       $meta_type = $this->meta_query->get_cast_for_type( $type );
+                                       $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
+                               } else {
+                                       $orderby = "$wpdb->postmeta.meta_value";
+                               }
+                               break;
+                       case 'meta_value_num':
+                               $orderby = "$wpdb->postmeta.meta_value+0";
+                               break;
+                       default:
+                               $orderby = "$wpdb->posts.post_" . $orderby;
+                               break;
+               }
+
+               return $orderby;
+       }
+
+       /**
+        * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
+        *
+        * @since 4.0.0
+        * @access protected
+        *
+        * @param string $order The 'order' query variable.
+        * @return string The sanitized 'order' query variable.
+        */
+       protected function parse_order( $order ) {
+               if ( ! is_string( $order ) || empty( $order ) ) {
+                       return 'DESC';
+               }
+
+               if ( 'ASC' === strtoupper( $order ) ) {
+                       return 'ASC';
+               } else {
+                       return 'DESC';
+               }
+       }
+
+       /**
</ins><span class="cx">    * Sets the 404 property and saves whether query is feed.
</span><span class="cx">   *
</span><span class="cx">   * @since 2.0.0
</span><span class="lines">@@ -2702,12 +2793,23 @@
</span><span class="cx"> 
</span><span class="cx">          $where .= $search . $whichauthor . $whichmimetype;
</span><span class="cx"> 
</span><del>-               if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
</del><ins>+                if ( ! isset( $q['order'] ) ) {
</ins><span class="cx">                   $q['order'] = 'DESC';
</span><ins>+               } else {
+                       $q['order'] = $this->parse_order( $q['order'] );
+               }
</ins><span class="cx"> 
</span><del>-               // Order by
-               if ( empty($q['orderby']) ) {
-                       $orderby = "$wpdb->posts.post_date " . $q['order'];
</del><ins>+                // Order by.
+               if ( empty( $q['orderby'] ) ) {
+                       /*
+                        * Boolean false or empty array blanks out ORDER BY,
+                        * while leaving the value unset or otherwise empty sets the default.
+                        */
+                       if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) {
+                               $orderby = '';
+                       } else {
+                               $orderby = "$wpdb->posts.post_date " . $q['order'];
+                       }
</ins><span class="cx">           } elseif ( 'none' == $q['orderby'] ) {
</span><span class="cx">                  $orderby = '';
</span><span class="cx">          } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
</span><span class="lines">@@ -2715,59 +2817,41 @@
</span><span class="cx">          } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
</span><span class="cx">                  $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
</span><span class="cx">          } else {
</span><del>-                       // Used to filter values
-                       $allowed_keys = array( 'name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count', 'type' );
-                       if ( !empty($q['meta_key']) ) {
-                               $allowed_keys[] = $q['meta_key'];
-                               $allowed_keys[] = 'meta_value';
-                               $allowed_keys[] = 'meta_value_num';
-                       }
-                       $q['orderby'] = urldecode($q['orderby']);
-                       $q['orderby'] = addslashes_gpc($q['orderby']);
-
</del><span class="cx">                   $orderby_array = array();
</span><del>-                       foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
-                               // Only allow certain values for safety
-                               if ( ! in_array($orderby, $allowed_keys) )
-                                       continue;
</del><ins>+                        if ( is_array( $q['orderby'] ) ) {
+                               foreach ( $q['orderby'] as $_orderby => $order ) {
+                                       $orderby = addslashes_gpc( urldecode( $_orderby ) );
+                                       $parsed  = $this->parse_orderby( $orderby );
</ins><span class="cx"> 
</span><del>-                               switch ( $orderby ) {
-                                       case 'menu_order':
-                                               $orderby = "$wpdb->posts.menu_order";
-                                               break;
-                                       case 'ID':
-                                               $orderby = "$wpdb->posts.ID";
-                                               break;
-                                       case 'rand':
-                                               $orderby = 'RAND()';
-                                               break;
-                                       case $q['meta_key']:
-                                       case 'meta_value':
-                                               if ( isset( $q['meta_type'] ) ) {
-                                                       $meta_type = $this->meta_query->get_cast_for_type( $q['meta_type'] );
-                                                       $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
-                                               } else {
-                                                       $orderby = "$wpdb->postmeta.meta_value";
-                                               }
-                                               break;
-                                       case 'meta_value_num':
-                                               $orderby = "$wpdb->postmeta.meta_value+0";
-                                               break;
-                                       case 'comment_count':
-                                               $orderby = "$wpdb->posts.comment_count";
-                                               break;
-                                       default:
-                                               $orderby = "$wpdb->posts.post_" . $orderby;
</del><ins>+                                        if ( ! $parsed ) {
+                                               continue;
+                                       }
+
+                                       $orderby_array[] = $parsed . ' ' . $this->parse_order( $order );
</ins><span class="cx">                           }
</span><ins>+                               $orderby = implode( ', ', $orderby_array );
</ins><span class="cx"> 
</span><del>-                               $orderby_array[] = $orderby;
</del><ins>+                        } else {
+                               $q['orderby'] = urldecode( $q['orderby'] );
+                               $q['orderby'] = addslashes_gpc( $q['orderby'] );
+
+                               foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
+                                       $parsed = $this->parse_orderby( $orderby );
+                                       // Only allow certain values for safety.
+                                       if ( ! $parsed ) {
+                                               continue;
+                                       }
+
+                                       $orderby_array[] = $parsed;
+                               }
+                               $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
+
+                               if ( empty( $orderby ) ) {
+                                       $orderby = "$wpdb->posts.post_date ".$q['order'];
+                               } else {
+                                       $orderby .= " {$q['order']}";
+                               }
</ins><span class="cx">                   }
</span><del>-                       $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
-
-                       if ( empty( $orderby ) )
-                               $orderby = "$wpdb->posts.post_date ".$q['order'];
-                       else
-                               $orderby .= " {$q['order']}";
</del><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          // Order search results by relevance only when another "orderby" is not specified in the query.
</span></span></pre></div>
<a id="trunktestsphpunittestspostqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/post/query.php (29026 => 29027)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/post/query.php 2014-07-08 17:14:20 UTC (rev 29026)
+++ trunk/tests/phpunit/tests/post/query.php    2014-07-08 17:15:53 UTC (rev 29027)
</span><span class="lines">@@ -761,4 +761,74 @@
</span><span class="cx">          $q3 = new WP_Query( array( 'post_status' => array( 'any', 'auto-draft' ) ) );
</span><span class="cx">          $this->assertNotContains( "post_status <> 'auto-draft'", $q3->request );
</span><span class="cx">  }
</span><ins>+
+       /**
+        *
+        * @ticket 17065
+        */
+       function test_orderby_array() {
+               global $wpdb;
+
+               $q1 = new WP_Query( array(
+                       'orderby' => array(
+                               'type' => 'DESC',
+                               'name' => 'ASC'
+                       )
+               ) );
+               $this->assertContains(
+                       "ORDER BY $wpdb->posts.post_type DESC, $wpdb->posts.post_name ASC",
+                       $q1->request
+               );
+
+               $q2 = new WP_Query( array( 'orderby' => array() ) );
+               $this->assertNotContains( 'ORDER BY', $q2->request );
+               $this->assertNotContains( 'ORDER', $q2->request );
+
+               $q3 = new WP_Query( array( 'post_type' => 'post' ) );
+               $this->assertContains(
+                       "ORDER BY $wpdb->posts.post_date DESC",
+                       $q3->request
+               );
+
+               $q4 = new WP_Query( array( 'post_type' => 'post' ) );
+               $this->assertContains(
+                       "ORDER BY $wpdb->posts.post_date DESC",
+                       $q4->request
+               );
+       }
+
+       /**
+        *
+        * @ticket 17065
+        */
+       function test_order() {
+               global $wpdb;
+
+               $q1 = new WP_Query( array(
+                       'orderby' => array(
+                               'post_type' => 'foo'
+                       )
+               ) );
+               $this->assertContains(
+                       "ORDER BY $wpdb->posts.post_type DESC",
+                       $q1->request
+               );
+
+               $q2 = new WP_Query( array(
+                       'orderby' => 'title',
+                       'order'   => 'foo'
+               ) );
+               $this->assertContains(
+                       "ORDER BY $wpdb->posts.post_title DESC",
+                       $q2->request
+               );
+
+               $q3 = new WP_Query( array(
+                       'order' => 'asc'
+               ) );
+               $this->assertContains(
+                       "ORDER BY $wpdb->posts.post_date ASC",
+                       $q3->request
+               );
+       }
</ins><span class="cx"> }
</span><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>