Make WordPress Core

Opened 6 years ago

Closed 2 years ago

#43599 closed enhancement (fixed)

WP is asking for comment count for post 0

Reported by: grzegorzjanoszka's profile Grzegorz.Janoszka Owned by:
Milestone: 5.3 Priority: normal
Severity: normal Version: 4.9.5
Component: Comments Keywords:
Focuses: Cc:

Description

In wp-includes/comment.php we have a function wp_count_comments defined:
function wp_count_comments( $post_id = 0 )
so by default $post_id is 0.
Later on in the function we query the cache:
$count = wp_cache_get( "comments-{$post_id}", 'counts' );

We shouldn't query the cache for such number if post_id is 0 as we will never get a value from the cache, so I suggest adding "if ($post_id) then" before querying the cache. It might be that more of this function code should be included in such if, being void for post_id equal to 0 (and there are such calls).

Change History (4)

#1 follow-up: @soulseekah
6 years ago

  • Keywords reporter-feedback added

The docblock does not explicitly state this, but calling wp_count_comments and get_comment_count with 0 (or no argument) returns the totals for the whole blog, so caching seems to actually be valid here.

#2 @Grzegorz.Janoszka
6 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Ah, a pity it was not in the docs that post_id=0 means all comments for the blog.
I hope it will be fixed.
And I haven't noticed counts was non-persistent group, so actually each WP page load generates cache miss for this one.
I am closing this ticket now.

#3 @soulseekah
6 years ago

  • Keywords needs-docs added; reporter-feedback removed
  • Resolution invalid deleted
  • Status changed from closed to reopened

The docblock should be fixed, I think to avoid such confusion in the future. Reopening.

#4 in reply to: ↑ 1 @SergeyBiryukov
2 years ago

  • Keywords needs-docs removed
  • Milestone changed from Awaiting Review to 5.3
  • Resolution set to fixed
  • Status changed from reopened to closed

Replying to soulseekah:

The docblock does not explicitly state this, but calling wp_count_comments and get_comment_count with 0 (or no argument) returns the totals for the whole blog, so caching seems to actually be valid here.

It looks like this was addressed in [46223] / #47110, both functions mention it in the DocBlock now:

@param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that
                    comment counts for the whole site will be retrieved.
Note: See TracTickets for help on using tickets.