188
180
* @return array List of comments.
190
182
function get_comments( $args = '' ) {
194
'author_email' => '',
209
$args = wp_parse_args( $args, $defaults );
210
extract( $args, EXTR_SKIP );
212
// $args can be whatever, only use the args defined in defaults to compute the key
213
$key = md5( serialize( compact(array_keys($defaults)) ) );
214
$last_changed = wp_cache_get('last_changed', 'comment');
215
if ( !$last_changed ) {
216
$last_changed = time();
217
wp_cache_set('last_changed', $last_changed, 'comment');
219
$cache_key = "get_comments:$key:$last_changed";
221
if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
225
$post_id = absint($post_id);
227
if ( 'hold' == $status )
228
$approved = "comment_approved = '0'";
229
elseif ( 'approve' == $status )
230
$approved = "comment_approved = '1'";
231
elseif ( 'spam' == $status )
232
$approved = "comment_approved = 'spam'";
233
elseif ( 'trash' == $status )
234
$approved = "comment_approved = 'trash'";
236
$approved = "( comment_approved = '0' OR comment_approved = '1' )";
238
$order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
240
if ( ! empty( $orderby ) ) {
241
$ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
242
$ordersby = array_intersect(
248
'comment_author_email',
250
'comment_author_url',
183
$query = new WP_Comment_Query;
184
return $query->query( $args );
188
* WordPress Comment Query class.
192
class WP_Comment_Query {
199
* @param string|array $query_vars
202
function query( $query_vars ) {
206
'author_email' => '',
262
$orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
264
$orderby = 'comment_date_gmt';
267
$number = absint($number);
268
$offset = absint($offset);
270
if ( !empty($number) ) {
272
$number = 'LIMIT ' . $offset . ',' . $number;
274
$number = 'LIMIT ' . $number;
282
if ( ! empty($post_id) )
283
$post_where .= $wpdb->prepare( 'comment_post_ID = %d AND ', $post_id );
284
if ( '' !== $author_email )
285
$post_where .= $wpdb->prepare( 'comment_author_email = %s AND ', $author_email );
287
$post_where .= $wpdb->prepare( 'comment_karma = %d AND ', $karma );
288
if ( 'comment' == $type )
289
$post_where .= "comment_type = '' AND ";
290
elseif ( ! empty( $type ) )
291
$post_where .= $wpdb->prepare( 'comment_type = %s AND ', $type );
292
if ( '' !== $parent )
293
$post_where .= $wpdb->prepare( 'comment_parent = %d AND ', $parent );
294
if ( '' !== $user_id )
295
$post_where .= $wpdb->prepare( 'user_id = %d AND ', $user_id );
297
$comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
298
wp_cache_add( $cache_key, $comments, 'comment' );
223
$this->query_vars = wp_parse_args( $query_vars, $defaults );
224
do_action_ref_array( 'pre_get_comments', array( &$this ) );
225
extract( $this->query_vars, EXTR_SKIP );
227
// $args can be whatever, only use the args defined in defaults to compute the key
228
$key = md5( serialize( compact(array_keys($defaults)) ) );
229
$last_changed = wp_cache_get('last_changed', 'comment');
230
if ( !$last_changed ) {
231
$last_changed = time();
232
wp_cache_set('last_changed', $last_changed, 'comment');
234
$cache_key = "get_comments:$key:$last_changed";
236
if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
240
$post_id = absint($post_id);
242
if ( 'hold' == $status )
243
$approved = "comment_approved = '0'";
244
elseif ( 'approve' == $status )
245
$approved = "comment_approved = '1'";
246
elseif ( 'spam' == $status )
247
$approved = "comment_approved = 'spam'";
248
elseif ( 'trash' == $status )
249
$approved = "comment_approved = 'trash'";
251
$approved = "( comment_approved = '0' OR comment_approved = '1' )";
253
$order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
255
if ( ! empty( $orderby ) ) {
256
$ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
257
$ordersby = array_intersect(
263
'comment_author_email',
265
'comment_author_url',
277
$orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
279
$orderby = 'comment_date_gmt';
282
$number = absint($number);
283
$offset = absint($offset);
285
if ( !empty($number) ) {
287
$limits = 'LIMIT ' . $offset . ',' . $number;
289
$limits = 'LIMIT ' . $number;
295
$fields = 'COUNT(*)';
302
if ( ! empty($post_id) )
303
$where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
304
if ( '' !== $author_email )
305
$where .= $wpdb->prepare( ' AND comment_author_email = %s', $author_email );
307
$where .= $wpdb->prepare( ' AND comment_karma = %d', $karma );
308
if ( 'comment' == $type ) {
309
$where .= " AND comment_type = ''";
310
} elseif( 'pings' == $type ) {
311
$where .= ' AND comment_type IN ("pingback", "trackback")';
312
} elseif ( ! empty( $type ) ) {
313
$where .= $wpdb->prepare( ' AND comment_type = %s', $type );
315
if ( '' !== $parent )
316
$where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
317
if ( '' !== $user_id )
318
$where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
319
if ( '' !== $search )
320
$where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
322
$pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
323
$clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
324
foreach ( $pieces as $piece )
325
$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
327
$query = "SELECT $fields FROM $wpdb->comments $join WHERE $where ORDER BY $orderby $order $limits";
330
return $wpdb->get_var( $query );
332
$comments = $wpdb->get_results( $query );
333
$comments = apply_filters_ref_array( 'the_comments', array( $comments, &$this ) );
335
wp_cache_add( $cache_key, $comments, 'comment' );
341
* Used internally to generate an SQL string for searching across multiple columns
346
* @param string $string
350
function get_search_sql( $string, $cols ) {
351
$string = esc_sql( like_escape( $string ) );
354
foreach ( $cols as $col )
355
$searches[] = "$col LIKE '%$string%'";
357
return ' AND (' . implode(' OR ', $searches) . ')';