Make WordPress Core

Opened 4 years ago

Last modified 3 years ago

#43599 reopened enhancement

WP is asking for comment count for post 0

Reported by: Grzegorz.Janoszka Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.5
Component: Comments Keywords: needs-docs
Focuses: Cc:


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 (3)

#1 @soulseekah
3 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
3 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
3 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.

Note: See TracTickets for help on using tickets.