Opened 16 months ago
Last modified 13 months ago
#19903 new enhancement
wp_count_comments() and get_comments_count() both do SQL queries — at Version 5
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Comments | Version: | 3.3.1 |
| Severity: | normal | Keywords: | |
| Cc: |
Description (last modified by markjaquith)
wp_count_comments() and get_comments_count() are similar functions, with a few differences:
- Their results are returned in different formats:
- wp_count_comments() returns an object of:
- spam
- approved
- moderated
- total_comments
- trash
- post-trashed
- get_comment_count() returns an array of:
- spam
- approved
- awaiting_moderation
- total_comments
- wp_count_comments() returns an object of:
- wp_count_comments() caches, but get_comment_count() always hits the database.
- get_comment_count() is used nowhere in WordPress core.
I propose that we add 'trash' and 'post-trashed' reporting to get_comment_count(), and then have wp_count_comments() use get_comment_count() instead of doing its own SQL queries.
Change History (6)
comment:1
FolioVision — 16 months ago
comment:2
markjaquith — 16 months ago
Wouldn't it make better sense to simply get rid of get_comment_count and make wp_count_comments count in the optimised way
Yeah, probably would. I think I was just thinking we needed both because I'd assumed that core needed the non-cached one, which isn't the case... it doesn't use it at all.
markjaquith — 16 months ago
comment:3
markjaquith — 16 months ago
Patch deprecates get_comment_count().
Could the deprecated version become a wrapper for wp_count_comments()? We'd simply need to rename the returned array keys.
comment:5
markjaquith — 16 months ago
- Description modified (diff)
Fixing the description to note that wp_count_comments() returns an object.
Could the deprecated version become a wrapper for wp_count_comments()? We'd simply need to rename the returned array keys.
Maybe. Unless people are using it to get "hot" data from the database. The cached counts might sometimes be wrong.

Hi Mark,
Wouldn't it make better sense to simply get rid of get_comment_count and make wp_count_comments count in the optimised way (see ticket #19901)?
Keeping them both around is very confusing, especially as get_comment_count is not used in core. Duplicate functions have been a bit of a bugbear for us.
Alec