Fix: Comments number is wrong - Core ticket - 36409#9277
Fix: Comments number is wrong - Core ticket - 36409#9277hbhalodia wants to merge 14 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. |
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 AND comment_type != 'note'", $post_id ) ); |
There was a problem hiding this comment.
Why not use get_comments( array( 'post_id' => $post_id, 'type__not_in' => array( 'note' ) ) )?
There was a problem hiding this comment.
Hi @westonruter, I was just going with consistency, Earlier it was using DB query only to fetch the comments, there is no as such reason to go with SQL query.
I will update and test the same.
There was a problem hiding this comment.
+1 what weston suggested.
There was a problem hiding this comment.
Hi @westonruter @mukeshpanchal27, Ideally the DB query was used here to prevent the unwanted scenarios that can arise when using get_comments, It can egarly loads all the comments meta of the comments, which fails the test added here --
andI have fixed it by adding an argument, 'update_comment_meta_cache' => false,. Let me know if this looks good, or else I can revert back to use DB query which won't be having any such side effects.
Thanks,
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.