-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix: Comments number is wrong - Core ticket - 36409 #9277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 2 commits
47cf2c2
4bef578
1da3ad1
01c7ce9
75dbcab
158061f
e64afba
1114e45
491e833
bc41c7e
e4513b8
086c901
b136e33
45d8fd7
97a887c
9129e38
6242f4c
b6689ff
c3cda23
e351f6f
c97aa78
e357266
7163df9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2812,7 +2812,59 @@ function wp_update_comment_count_now( $post_id ) { | |
| $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id ); | ||
|
|
||
| if ( is_null( $new ) ) { | ||
| $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) ); | ||
| /** | ||
| * Get all the comments related to the post ID. | ||
| */ | ||
| $comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_parent, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This approach could be too expensive if there are a large number of comments. Have you tested this with, for example, 1ok unapproved comments? It would be good to understand the performance impact at that scale.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have not tested this scenario with that number of comments, I will check for the same. Also, I am not sure if we have a better solution than this? because everything is in one table like the replationships as well, SQL query would be also expensive in this case and would not cover all the deeply nested comments. We can also do some caching with array mechanism to skip the parents that are already checked in the current approach?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @mukeshpanchal27, I tested with 10K comments and it looks like it is not performant heavy, I have recorder the screencast below with the changes in PR and it looks good with not performant heavy in counting the comments, ofCourse it would take time when the actual comments are being rendered on the page. Screen.Recording.2025-07-17.at.2.39.26.PM.movThank You, |
||
| $comments_by_id = array(); | ||
|
|
||
| /** | ||
| * Create a lookup array by comment ID. | ||
| */ | ||
| foreach ( $comments as $comment ) { | ||
| $comments_by_id[ $comment->comment_ID ] = $comment; | ||
| } | ||
|
|
||
| // Count for comment. | ||
|
hbhalodia marked this conversation as resolved.
Outdated
|
||
| $comment_count = 0; | ||
|
|
||
| /** | ||
| * Loop through each comment and check for approved comment. | ||
| */ | ||
|
hbhalodia marked this conversation as resolved.
Outdated
|
||
| foreach ( $comments as $comment ) { | ||
|
|
||
| // Proceed only if comment is approved for counting. | ||
| if ( '1' !== $comment->comment_approved ) { | ||
| continue; | ||
| } | ||
|
|
||
| $parent_id = (int) $comment->comment_parent; | ||
| $has_unapproved = false; | ||
|
|
||
| /** | ||
| * Check until we get the parent id as 0. | ||
| */ | ||
|
hbhalodia marked this conversation as resolved.
Outdated
|
||
| while ( 0 !== $parent_id ) { | ||
| if ( ! isset( $comments_by_id[ $parent_id ] ) ) { | ||
| break; | ||
|
hbhalodia marked this conversation as resolved.
|
||
| } | ||
|
|
||
| $parent_comment = $comments_by_id[ $parent_id ]; | ||
|
|
||
| if ( '1' !== $parent_comment->comment_approved ) { | ||
| $has_unapproved = true; | ||
| break; | ||
| } | ||
|
|
||
| $parent_id = (int) $parent_comment->comment_parent; | ||
| } | ||
|
|
||
| if ( ! $has_unapproved ) { | ||
| $comment_count++; | ||
|
hbhalodia marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| $new = $comment_count; | ||
| } else { | ||
| $new = (int) $new; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.