Skip to content

Comments: Replace get_comments() with get_comments_number(). #50798

Merged
cbravobernal merged 1 commit intoWordPress:trunkfrom
mboynes:patch-1
May 24, 2023
Merged

Comments: Replace get_comments() with get_comments_number(). #50798
cbravobernal merged 1 commit intoWordPress:trunkfrom
mboynes:patch-1

Conversation

@mboynes
Copy link
Copy Markdown
Contributor

@mboynes mboynes commented May 19, 2023

What?

This PR replaces a get_comments() call and database query with get_comments_number().

Why?

This approach has three advantages:

  1. It's faster. get_comments_number() uses a post property vs a separate database query.
  2. get_comments_number() is filtered, and it's much easier for plugins/themes to manipulate, vs having to use pre_get_comments and jump through several hoops.
  3. This is consistent with the way core themes used to determine whether or not to render the comments template, from twentyten up to twentytwentyone. Maybe even older, I didn't check Kubrick 😄.

Testing Instructions

Using the comments block under normal circumstances, no behavior changes should be observable.

To disabled comments and commenting on all content, one should now be able to:

add_filter( 'comments_open', '__return_false' );
add_filter( 'get_comments_number', '__return_zero' );

Observe that prior to this PR, comments will still render using the comments block.

Closes #48975

@mboynes mboynes requested a review from michalczaplinski as a code owner May 19, 2023 20:25
Copy link
Copy Markdown
Contributor

@alexstine alexstine left a comment

Choose a reason for hiding this comment

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

Code looks reasonable to me.

@gziolo gziolo added [Type] Performance Related to performance efforts [Block] Comments Affects the Comments Block - formerly known as Comments Query Loop labels May 20, 2023
@gziolo gziolo requested review from cbravobernal and ockham May 23, 2023 20:39
Copy link
Copy Markdown
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

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

Tested and work as expected.

@cbravobernal cbravobernal merged commit 5f2d8a8 into WordPress:trunk May 24, 2023
@github-actions github-actions bot added this to the Gutenberg 15.9 milestone May 24, 2023
@cbravobernal cbravobernal added [Type] Code Quality Issues or PRs that relate to code quality [Type] Performance Related to performance efforts and removed [Type] Performance Related to performance efforts labels May 24, 2023
@cbravobernal cbravobernal changed the title Replace get_comments() with get_comments_number() in comments block Comments: Replace get_comments() with get_comments_number(). May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Comments Affects the Comments Block - formerly known as Comments Query Loop [Type] Code Quality Issues or PRs that relate to code quality [Type] Performance Related to performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The comments block does not apply get_comments_number filter to determine what to show

4 participants