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: http://svn.automattic.com/wordpress/trunk@17667 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2011-04-20 18:02:41 +00:00
parent cefee535db
commit 430bb76d4b
2 changed files with 13 additions and 1 deletions

View File

@ -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 )

View File

@ -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;