Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#27018 closed defect (bug) (duplicate)

pre_get_comments doesn't trigger when calling comments_template() for logged in users

Reported by: flynsarmy's profile flynsarmy Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8.1
Component: Comments Keywords:
Focuses: template Cc:


When comments_template() function is called for a logged in user, the pre_get_comments action doesn't trigger. This is caused by the calling of $wpdb->get_results() directly in the comments_template() function so WP_Comments_Query (where the action is defined) is never reached:

/** @todo Use API instead of SELECTs. */
if ( $user_ID) {
	$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID));
} else if ( empty($comment_author) ) {
	$comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
} else {
	$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));

Change History (2)

#1 @bcworkz
10 years ago

It appears the reason WP_Comment_Query is not used for logged in users (or not logged in users who made a comment) is the class does not support including unapproved comments from the current user. If this feature were supported, then WP_Comment_Query could be used for all conditions. This is addressed by the related #19623.

#2 @SergeyBiryukov
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #19623.

Note: See TracTickets for help on using tickets.