From f888f1cc64160088a25d4df7952d617d57193548 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Wed, 20 Apr 2011 18:02:41 +0000 Subject: [PATCH] Allow retrieving comments by post type, status, author, author, name, or parent. Fetch only published posts for recent comments widget. Props filosofo. fixes #16506 #12904 git-svn-id: https://develop.svn.wordpress.org/trunk@17667 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/comment.php | 12 ++++++++++++ wp-includes/default-widgets.php | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 08919f3158..3dea3be15b 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -213,6 +213,11 @@ class WP_Comment_Query { 'parent' => '', 'post_ID' => '', 'post_id' => 0, + 'post_author' => '', + 'post_name' => '', + 'post_parent' => '', + 'post_status' => '', + 'post_type' => '', 'status' => '', 'type' => '', 'user_id' => '', @@ -319,6 +324,13 @@ class WP_Comment_Query { if ( '' !== $search ) $where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) ); + $post_fields = array_filter( compact( array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type', ) ) ); + if ( ! empty( $post_fields ) ) { + $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; + foreach( $post_fields as $field_name => $field_value ) + $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value ); + } + $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' ); $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); foreach ( $pieces as $piece ) diff --git a/wp-includes/default-widgets.php b/wp-includes/default-widgets.php index 8f3ad310f7..dc1c9ec880 100644 --- a/wp-includes/default-widgets.php +++ b/wp-includes/default-widgets.php @@ -640,7 +640,7 @@ class WP_Widget_Recent_Comments extends WP_Widget { if ( ! $number = absint( $instance['number'] ) ) $number = 5; - $comments = get_comments( array( 'number' => $number, 'status' => 'approve' ) ); + $comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' ) ); $output .= $before_widget; if ( $title ) $output .= $before_title . $title . $after_title;