<!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>[12518] trunk/wp-includes/comment.php:
  Add more orderby and select options to get_comments.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12518">12518</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2009-12-23 16:16:13 +0000 (Wed, 23 Dec 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add more orderby and select options to get_comments. Props filosofo. fixes <a href="http://trac.wordpress.org/ticket/10668">#10668</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludescommentphp">trunk/wp-includes/comment.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludescommentphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/comment.php (12517 => 12518)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/comment.php        2009-12-23 16:03:04 UTC (rev 12517)
+++ trunk/wp-includes/comment.php        2009-12-23 16:16:13 UTC (rev 12518)
</span><span class="lines">@@ -185,7 +185,21 @@
</span><span class="cx"> function get_comments( $args = '' ) {
</span><span class="cx">         global $wpdb;
</span><span class="cx"> 
</span><del>-        $defaults = array('status' =&gt; '', 'orderby' =&gt; 'comment_date_gmt', 'order' =&gt; 'DESC', 'number' =&gt; '', 'offset' =&gt; '', 'post_id' =&gt; 0);
</del><ins>+        $defaults = array(
+                'author_email' =&gt; '',
+                'ID' =&gt; '',
+                'karma' =&gt; '',
+                'number' =&gt; '', 
+                'offset' =&gt; '', 
+                'orderby' =&gt; '', 
+                'order' =&gt; 'DESC', 
+                'parent' =&gt; '',
+                'post_ID' =&gt; '',
+                'post_id' =&gt; 0,
+                'status' =&gt; '', 
+                'type' =&gt; '',
+                'user_id' =&gt; '',
+        );
</ins><span class="cx"> 
</span><span class="cx">         $args = wp_parse_args( $args, $defaults );
</span><span class="cx">         extract( $args, EXTR_SKIP );
</span><span class="lines">@@ -218,7 +232,32 @@
</span><span class="cx"> 
</span><span class="cx">         $order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
</span><span class="cx"> 
</span><del>-        $orderby = 'comment_date_gmt';  // Hard code for now
</del><ins>+        if ( ! empty( $orderby ) ) {
+                $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
+                $ordersby = array_intersect(
+                        $ordersby, 
+                        array(
+                                'comment_agent',
+                                'comment_approved',
+                                'comment_author',
+                                'comment_author_email',
+                                'comment_author_IP',
+                                'comment_author_url',
+                                'comment_content',
+                                'comment_date',
+                                'comment_date_gmt',
+                                'comment_ID',
+                                'comment_karma',
+                                'comment_parent',
+                                'comment_post_ID',
+                                'comment_type',
+                                'user_id',
+                        )
+                );
+                $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
+        } else {
+                $orderby = 'comment_date_gmt';
+        }
</ins><span class="cx"> 
</span><span class="cx">         $number = absint($number);
</span><span class="cx">         $offset = absint($offset);
</span><span class="lines">@@ -233,10 +272,22 @@
</span><span class="cx">                 $number = '';
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        $post_where = '';
+
</ins><span class="cx">         if ( ! empty($post_id) )
</span><del>-                $post_where = $wpdb-&gt;prepare( 'comment_post_ID = %d AND', $post_id );
-        else
-                $post_where = '';
</del><ins>+                $post_where .= $wpdb-&gt;prepare( 'comment_post_ID = %d AND ', $post_id );
+        if ( '' !== $author_email ) 
+                $post_where .= $wpdb-&gt;prepare( 'comment_author_email = %s AND ', $author_email );
+        if ( '' !== $karma )
+                $post_where .= $wpdb-&gt;prepare( 'comment_karma = %d AND ', $karma );
+        if ( 'comment' == $type )
+                $post_where .= &quot;comment_type = '' AND &quot;;
+        elseif ( ! empty( $type ) ) 
+                $post_where .= $wpdb-&gt;prepare( 'comment_type = %s AND ', $type );
+        if ( '' !== $parent )
+                $post_where .= $wpdb-&gt;prepare( 'comment_parent = %d AND ', $parent );
+        if ( '' !== $user_id )
+                $post_where .= $wpdb-&gt;prepare( 'user_id = %d AND ', $user_id );
</ins><span class="cx"> 
</span><span class="cx">         $comments = $wpdb-&gt;get_results( &quot;SELECT * FROM $wpdb-&gt;comments WHERE $post_where $approved ORDER BY $orderby $order $number&quot; );
</span><span class="cx">         wp_cache_add( $cache_key, $comments, 'comment' );
</span></span></pre>
</div>
</div>

</body>
</html>