Fix: Comments number is wrong - Core ticket - 36409#9277
Fix: Comments number is wrong - Core ticket - 36409#9277hbhalodia wants to merge 21 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
src/wp-includes/comment.php
Outdated
| /** | ||
| * 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 ) ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.mov
Thank You,
|
Can we get some phpunit tests here to show this fixes the bug and to prevent regressions? |
Hi @mindctrl, I have added the test cases for the updated scenario. Thank You, |
|
Can we mention ticket number in unit test? |
|
@hbhalodia, Since the failure occurred in the CI, I merged the latest trunk into this pull request. |
There was a problem hiding this comment.
Pull request overview
This PR addresses Core ticket #36409 by changing how wp_update_comment_count_now() recalculates a post’s comment_count, aiming to exclude approved child comments when any ancestor comment is unapproved/trashed.
Changes:
- Replace the direct SQL
COUNT(*)inwp_update_comment_count_now()with PHP logic that checks approval status across a comment’s ancestor chain. - Add PHPUnit coverage for cases where unapproved/trashed parent comments should cause approved children to be excluded from the stored comment count.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
src/wp-includes/comment.php |
Reworks comment counting to exclude approved descendants of unapproved ancestors (and still exclude note comment types). |
tests/phpunit/tests/comment/wpUpdateCommentCountNow.php |
Adds tests covering trashed/unapproved parents excluding child comments from comment_count. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Trac ticket: https://core.trac.wordpress.org/ticket/36409
Fixes
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.