Make WordPress Core

Opened 5 weeks ago

Last modified 2 weeks ago

#66260 new enhancement

Comments: Hide zero-count status filters on the Comments list table

Reported by: abditsori Owned by:
Priority: normal Milestone: 7.3
Component: Comments Version:
Severity: normal Keywords: has-patch has-unit-tests 2nd-opinion
Cc: Focuses: administration

Description

The Posts list table (WP_Posts_List_Table::get_views()) hides any status
filter link — other than "All" — whose post count is zero:

if ( ! in_array( $status_name, $avail_post_stati, true ) || empty( $num_posts->$status_name ) ) {
	continue;
}

WP_Comments_List_Table::get_views() has no equivalent check. As a result,
"Pending", "Approved", "Spam", "Trash", and "Mine" are always rendered as
filter links above the Comments list table, even when their count is (0).

On a typical site with few or no pending/spam/trashed comments, this clutters
the screen with filter links that lead to an empty list, rather than only
showing filters that are actually useful.

Steps to reproduce:

  1. Go to a site where every comment is approved (no pending, spam, or trashed comments).
  2. Go to Comments in wp-admin.
  3. Observe the filter row above the list: "All | Mine | Pending (0) | Approved (12) | Spam (0) | Trash (0)".

Expected behavior: filters with a zero count, other than "All", should not
be shown — consistent with how the Posts list table already behaves for
post statuses.

Attachments (2)

Comments-Administration-WP7.1.3-and-Trunk.mp4​ (615.2 KB ) - added by sabernhardt 5 weeks ago.
Deleting and restoring a comment and a post in WordPress 7.1.3, then deleting and restoring a comment in Trunk with the patch
Screen Recording 2026-10-08 at 9.47.18 at night.mov​ (8.4 MB ) - added by abditsori 5 weeks ago.

Change History (11)

This ticket was mentioned in ​PR #14056 on ​WordPress/wordpress-develop by ​@abditsori.


5 weeks ago
#1

  • Keywords has-patch has-unit-tests added

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

Summary

WP_Posts_List_Table::get_views() hides any status filter link (other than "All") whose post count is zero:

if ( ! in_array( $status_name, $avail_post_stati, true ) || empty( $num_posts->$status_name ) ) {
        continue;
}

WP_Comments_List_Table::get_views() had no equivalent check, so "Pending", "Approved", "Spam", "Trash", and "Mine" were always rendered as filter links even when their count is (0), cluttering the screen with filters that return no results.

Changes

  • In class-wp-comments-list-table.php, skip adding a comment status link (other than all) when its count is empty, mirroring the Posts list table's logic.
  • Removed test_get_views_should_return_views_by_default from wpCommentsListTable.php, since it explicitly asserted the old behavior (all statuses shown at (0)), which this change intentionally reverses. No replacement test is included in this PR.

Test plan

  • [x] php -l on both changed files.
  • [x] Ran the remaining tests in Tests_Admin_wpCommentsListTable — 9 tests, 24 assertions, all passing.

🤖 Generated with ​Claude Code

#2 follow-up: @therssoftware
5 weeks ago

Test & Review Report

Tested ​PR #14056 locally against trunk.

Environment

  • WordPress: 7.2-alpha-63166-src (trunk)
  • PHP: 8.3.33
  • Database: MariaDB 10.11
  • PHPUnit: 9.6.37
  • PHPCS: 3.13.5 (WordPress-Core)

Testing Results

  1. Zero / Low-count views:
    • On a site with 0 comments, only All (0) is rendered.
    • On a site with only approved comments (e.g. 5 approved, 0 pending, 0 spam, 0 trash), only All (5) | Approved (5) are rendered.
  1. Active Tab Disappearance (Edge Case):
    • If an admin navigates to edit-comments.php?comment_status=moderated (e.g. from an email notification for a comment that was approved by another moderator) or ?comment_status=spam (after clicking "Empty Spam"), the moderated / spam view is skipped because its count is 0.
    • Result: The user is on ?comment_status=moderated, but the Pending tab is completely absent from the navigation bar, and none of the remaining links (All, Approved) have class="current". The user has no visual indicator of what status is currently filtered.
    • At a minimum, the currently active view should never be omitted:
      if ( 'all' !== $status && empty( $num_comments->$status ) && $status !== $comment_status ) {
          continue;
      }
      
  1. Workflow Considerations (Comments vs Posts):
    • While WP_Posts_List_Table hides empty statuses like Drafts and Trash, WP_Comments_List_Table serves a fundamentally different purpose centered on moderation.
    • Pending (0) is an active task queue where (0) provides reassuring confirmation of "Inbox Zero". If the tab disappears, administrators may wonder if comment moderation was disabled or broken.
    • Spam (0) provides visibility into whether spam filtering is operating and allows moderators to check for false positives without waiting for a new spam comment to cause the tab to suddenly appear. Dynamically adding/removing tabs causes the navigation bar to jump.
  1. Filter Backward Compatibility (comment_status_links):
    • The comment_status_links filter DocBlock explicitly documents that $status_links: Includes 'All', 'Mine', 'Pending', 'Approved', 'Spam', and 'Trash'.
    • Plugins manipulating these links will encounter Undefined array key notices in PHP 8+ if keys are omitted dynamically.
  1. Reversal of Ticket #42066:
    • In WP 6.1 (#42066), test_get_views_should_return_views_by_default() was added to explicitly protect the contract that all views are present by default with 0 counts.

Recommendation

While keeping list tables consistent is a good goal, edit-comments.php is primarily a moderation queue where zero-state visibility is informative. If any zero-count filtering is pursued, moderated and approved should remain permanent, and the currently requested status ($comment_status) must never be hidden.

Props therssoftware.


AI Disclosure: In accordance with the WordPress AI policy, I disclose that generative AI (Google Antigravity) was used in testing, reproduction, and drafting this review report. All test executions, script verifications, and comparisons were performed and validated locally in the development environment.

#3 in reply to: ↑ 2 @abditsori
5 weeks ago

@therssoftware Thanks for the feedback! I have updated the PR to address the concerns you had on it.

@sabernhardt
5 weeks ago

Deleting and restoring a comment and a post in WordPress 7.1.3, then deleting and restoring a comment in Trunk with the patch

#4 @sabernhardt
5 weeks ago

  • Focuses administration added
  • Keywords 2nd-opinion added

While keeping list tables consistent is a good goal, edit-comments.php is primarily a moderation queue where zero-state visibility is informative.

The Comments page is not consistent with other list table pages. It uses Ajax to update the items in the table and the filter links; post lists reload the page when the status changes.

For the video shows:

  • Deleting and restoring a comment in WordPress 7.1.3, where the filter links include zero counts, and those numbers updated from 1 for All and Approved to 1 for Trash and zero for the rest.
  • Deleting and restoring a post in WordPress 7.1.3, where the entire page reloads.
  • Deleting and restoring a comment in Trunk with PR 14056, which hides removes the "Spam (0)" and "Trash (0)" links. When I deleted the comment, the All and Approved counts decreased (the comment was not mine), but there was no link for the comment in Trash.
Last edited 5 weeks ago by sabernhardt (previous) (diff)

#5 follow-up: @therssoftware
5 weeks ago

Replying to sabernhardt:

The Comments page is not consistent with other list table pages. It uses Ajax to update the items in the table and the filter links; post lists reload the page when the status changes.
When I deleted the comment, the All and Approved counts decreased (the comment was not mine), but there was no link for the comment in Trash.

Thank you @sabernhardt for the testing video and for highlighting the core architectural difference here.

This illustrates why WP_Comments_List_Table cannot simply mirror WP_Posts_List_Table:

  1. Ajax Lifecycle in edit-comments.js: Unlike the Posts screen which performs a full page reload upon trashing, edit-comments.php updates counts in-place using updateCountText( 'span.trash-count', 1 ) and updateCountText( 'span.spam-count', 1 ). If Trash (0) or Spam (0) is omitted from initial server-side rendering, those DOM elements do not exist, and the Ajax handler has no view link to update or display. Trashing an item leaves the user with no UI link to navigate to Trash or restore the comment without manually modifying the URL or hard-refreshing the page.
  2. Complexity Trade-off: Supporting dynamic zero-count hiding on the Comments screen would require rewriting edit-comments.js to dynamically generate, insert, and tear down HTML <li> view links and delimiters in response to Ajax actions, adding substantial client-side overhead for questionable utility.
  3. Established Intent (Ticket #42066): As noted in comment:2, unit tests were specifically added in WP 6.1 under #42066 to protect the presence of all default views with 0 counts precisely to maintain stability for this moderation interface.

Given this Ajax dependency and the distinct moderation workflow of comments, keeping all default view links visible appears to be intentional and necessary by design. Unless there is appetite for a broader client-side overhaul of the comments Ajax system, closing this enhancement as wontfix may be the cleanest resolution.

#6 in reply to: ↑ 5 @abditsori
5 weeks ago

Replying to therssoftware:

Replying to sabernhardt:

The Comments page is not consistent with other list table pages. It uses Ajax to update the items in the table and the filter links; post lists reload the page when the status changes.
When I deleted the comment, the All and Approved counts decreased (the comment was not mine), but there was no link for the comment in Trash.

Thank you @sabernhardt for the testing video and for highlighting the core architectural difference here.

This illustrates why WP_Comments_List_Table cannot simply mirror WP_Posts_List_Table:

  1. Ajax Lifecycle in edit-comments.js: Unlike the Posts screen which performs a full page reload upon trashing, edit-comments.php updates counts in-place using updateCountText( 'span.trash-count', 1 ) and updateCountText( 'span.spam-count', 1 ). If Trash (0) or Spam (0) is omitted from initial server-side rendering, those DOM elements do not exist, and the Ajax handler has no view link to update or display. Trashing an item leaves the user with no UI link to navigate to Trash or restore the comment without manually modifying the URL or hard-refreshing the page.
  2. Complexity Trade-off: Supporting dynamic zero-count hiding on the Comments screen would require rewriting edit-comments.js to dynamically generate, insert, and tear down HTML <li> view links and delimiters in response to Ajax actions, adding substantial client-side overhead for questionable utility.
  3. Established Intent (Ticket #42066): As noted in comment:2, unit tests were specifically added in WP 6.1 under #42066 to protect the presence of all default views with 0 counts precisely to maintain stability for this moderation interface.

Given this Ajax dependency and the distinct moderation workflow of comments, keeping all default view links visible appears to be intentional and necessary by design. Unless there is appetite for a broader client-side overhaul of the comments Ajax system, closing this enhancement as wontfix may be the cleanest resolution.

@therssoftware I have updated the PR to address the concerns shared.

#7 @therssoftware
5 weeks ago

Replying to abditsori:

Thanks @abditsori for working to address the Ajax update issue!

While re-rendering .subsubsub over Ajax works in isolation on a clean install, doing so inside _wp_ajax_delete_comment_response() introduces a significant architectural bug for plugins:

  1. Plugin View Erasure (load-edit-comments.php): Standard practice for plugins extending edit-comments.php is to hook the views_edit-comments filter inside add_action( 'load-edit-comments.php', ... ). During Ajax requests to admin-ajax.php, the screen action load-edit-comments.php is never fired. As a result, when _wp_ajax_delete_comment_response() calls $wp_list_table->views(), it runs without any plugin-registered views. Executing $( '.subsubsub' ).replaceWith(...) on the client will wipe out all third-party tabs from the navigation bar the moment an admin trashes, spams, or approves a comment.
  1. Omitted $comment_type Global: WP_Comments_List_Table::get_views() relies on global $post_id, $comment_status, $comment_type; (line 253). The Ajax handler parses $post_id and $comment_status, but omits $comment_type. If an administrator is viewing comments filtered by type (?comment_type=...), the re-rendered tabs strip that parameter from the links.
  1. Complexity & Core Precedent (Ticket #42066): This demonstrates why edit-comments.php has historically kept all status views permanently rendered at (0) count (protected by test_get_views_should_return_views_by_default since WP 6.1). Modifying client-side list table DOM trees dynamically over Ajax adds substantial fragility to achieve a cosmetic change.

If the goal is purely visual de-cluttering without breaking the Ajax lifecycle or plugin extensibility, a safer alternative would be keeping all view links in the DOM and toggling their visibility via CSS classes (e.g. .is-empty-view { display: none; } which edit-comments.js can simply toggle when the count reaches 0 or > 0), rather than omitting them from the PHP array and re-rendering HTML over Ajax.

#8 @abditsori
5 weeks ago

Thank you for the feedback, @therssoftware! I have switched to DOM & CSS based approach now in the PR.

#9 @abditsori
2 weeks ago

  • Milestone Awaiting Review → 7.3
Note: See TracTickets for help on using tickets.