Skip to content

Fix: Comments number is wrong - Core ticket - 36409#9277

Open
hbhalodia wants to merge 14 commits intoWordPress:trunkfrom
hbhalodia:fix/issue-36409-comment-count
Open

Fix: Comments number is wrong - Core ticket - 36409#9277
hbhalodia wants to merge 14 commits intoWordPress:trunkfrom
hbhalodia:fix/issue-36409-comment-count

Conversation

@hbhalodia
Copy link

Trac ticket: https://core.trac.wordpress.org/ticket/36409

Fixes

  1. Get all the comments related to post and created the lookup based on the comment ID.
  2. Loop through each comment and check for its child<-->parent and add only if parent comment is approved.

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.

@github-actions
Copy link

github-actions bot commented Jul 17, 2025

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props hbhalodia, wildworks, mukesh27, westonruter, mindctrl, shailu25.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

/**
* 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 ) );
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.mov

Thank You,

@mindctrl
Copy link

mindctrl commented Nov 3, 2025

Can we get some phpunit tests here to show this fixes the bug and to prevent regressions?

@hbhalodia
Copy link
Author

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,

@shail-mehta
Copy link
Member

Can we mention ticket number in unit test?

@t-hamano
Copy link
Contributor

t-hamano commented Nov 7, 2025

@hbhalodia, Since the failure occurred in the CI, I merged the latest trunk into this pull request.

/**
* 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 ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use get_comments( array( 'post_id' => $post_id, 'type__not_in' => array( 'note' ) ) )?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 what weston suggested.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 --

public function test_update_comment_meta_cache_should_default_to_lazy_loading() {
and
public function test_update_comment_meta_cache_should_default_to_lazy_loading_fields_id() {

I 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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

Comments