WordPress.org

Make WordPress Core

Opened 10 years ago

Closed 5 years ago

#14809 closed defect (bug) (fixed)

comments_by_type doesn't always get reset

Reported by: nmassey Owned by: wonderboymusic
Milestone: 4.4 Priority: normal
Severity: minor Version: 3.0.1
Component: Comments Keywords: has-patch needs-testing
Focuses: Cc:

Description

in wp-includes/comment-template.php , in function comments_template(), it is possible for $wp_query->comments to be set but for $wp_query->comments_by_type NOT to get reset when it should be reset to array(). We ran into a bug because:

  • on a single page (actually inside of a widget), we were pulling comments from multiple posts
  • for each post, we were requesting get_comments(array('type' => 'comment'))
  • in wp-includes/comment-template.php , in function wp_list_comments(), code says:
    		if ( 'all' != $r['type'] ) {
    			if ( empty($wp_query->comments_by_type) )
    				$wp_query->comments_by_type = &separate_comments($wp_query->comments);
    			if ( empty($wp_query->comments_by_type[$r['type']]) )
    				return;
    			$_comments = $wp_query->comments_by_type[$r['type']];
    
    

unfortunately, since we were running that again and again, it would not recalculate $wp_query->comments_by_type each time.

I think that the function comments_template() in wp-includes/comment-template.php should have the following line added:

     $wp_query->comments_by_type = array();

right after the line where $wp_query->comments is set.

Attachments (1)

14809.patch (757 bytes) - added by jakub.tyrcha 10 years ago.

Download all attachments as: .zip

Change History (9)

#1 @dd32
10 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

#2 @solarissmoke
10 years ago

  • Keywords reporter-feedback added; needs-patch removed

I'm a bit confused here - if you were using get_comments() for each post and then supplying those as an argument to wp_list_comments(), the lines of code containing $wp_query->comments_by_type would never be invoked - they only get used if you don't supply your own list of comments.

Could you post the code you're using for get_comments() and wp_list_comments() in the loop?

@jakub.tyrcha
10 years ago

#3 @jakub.tyrcha
10 years ago

@up as far as I understand, he didn't supply the comments as arguments, and the result of the get_comments() call was used

#4 @iseulde
8 years ago

  • Keywords has-patch added

#5 @chriscct7
6 years ago

#15905 was marked as a duplicate.

#6 @rachelbaker
6 years ago

  • Keywords needs-testing added; reporter-feedback removed

#7 @wonderboymusic
5 years ago

  • Milestone changed from Future Release to 4.4

#8 @wonderboymusic
5 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 34503:

Comments: In comments_template(), set $wp_query->comments_by_type to empty array if not separating comments. The value becomes stale otherwise. Edge case.

Props jakub.tyrcha.
Fixes #14809.

Note: See TracTickets for help on using tickets.