I’m developing a plugin that is using the comments table with custom comment_type
for CPT replies. The problem was that, I’s getting replies to my CPT in Comments page (/wp-admin/edit-comments.php
). So I used a filter to put ’em out of the comments list. It’s working fine, and stripping my replies to CPT out of the list table (even in trash). But the comments (replies) that in trash is showing a count, if I deactivate my filter those comments (replies) are there, so the count is real.
As you can see in this screenshot that, my filter is active, all my CPT replies are hidden, and only a single trashed comment in the list that is from post type post
, but the count is showing the real data (3) as I have 2 trashed CPT replies.
But I want the count to be real with the view. As the view does contain a single comment (because I’m hiding the CPT replies with a custom filter), I need to show the count as of the view (1). I’m struggling finding an appropriate filter for that.
So, how can I control the comment counts (in pending, spam, and trash) filtering my CPT replies?
Here are three different methods to modify the trash count, to 999 as an example:
Method #1
The views_edit-comments
filter:
add_filter( 'views_edit-comments', function( $views )
{
$trash_count = 999; // <-- Adjust this count
// Override the 'trash' link:
$views['trash'] = sprintf(
"<a href=%s>%s <span class="count">(<span class="trash-count">%d</span>)</span></a>",
esc_url( admin_url( 'edit-comments.php?comment_status=trash') ),
__( 'Trash' ),
$trash_count
);
return $views;
} );
Method #2
The comment_status_links
filter:
add_filter( 'comment_status_links', function( $status_links )
{
$trash_count = 999; // <-- Adjust this count
// Override the 'trash' link:
$status_links['trash'] = sprintf(
"<a href=%s>%s <span class="count">(<span class="trash-count">%d</span>)</span></a>",
esc_url( admin_url( 'edit-comments.php?comment_status=trash') ),
__( 'Trash' ),
$trash_count
);
return $status_links;
} );
Method #3
Here we target the edit-comments.php
screen and adjust the corresponding instance of the wp_count_comments()
function:
add_filter( 'load-edit-comments.php', function()
{
add_filter( 'wp_count_comments', function( $stats, $post_id )
{
static $instance = 0;
if( 2 === $instance++ )
{
$stats = wp_count_comments( $stats, $post_id );
// Set the trash count to 999
if ( is_object( $stats ) && property_exists( $stats, 'trash' ) )
$stats->trash = 999; // <-- Adjust this count
}
return $stats;
}, 10, 2 );
} );
Similarly for the pending and spam counts.