The recent comments list is part of the Activity Dashboard Widget.
Approach #1
We could remove that dashboard widget and then add our modified version of it:
/**
* Remove the latest comments from the Activity dashboard widget (approach #1)
*/
add_action( 'wp_dashboard_setup', function()
{
// Remove the Activity widget
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
// Add the modified Activity widget
if ( is_blog_admin() )
wp_add_dashboard_widget(
'wpse_dashboard_activity',
__( 'Activity' ),
'wpse_dashboard_site_activity'
);
} );
where our custom callback (without comments) is:
/**
* Custom Activity Widget callback
*
* @see wp_dashboard_site_activity()
*/
function wpse_dashboard_site_activity() {
echo '<div id="activity-widget">';
$future_posts = wp_dashboard_recent_posts( array(
'max' => 5,
'status' => 'future',
'order' => 'ASC',
'title' => __( 'Publishing Soon' ),
'id' => 'future-posts',
) );
$recent_posts = wp_dashboard_recent_posts( array(
'max' => 5,
'status' => 'publish',
'order' => 'DESC',
'title' => __( 'Recently Published' ),
'id' => 'published-posts',
) );
// No comments!
$recent_comments = false; // wp_dashboard_recent_comments();
if ( !$future_posts && !$recent_posts && !$recent_comments ) {
echo '<div class="no-activity">';
echo '<p class="smiley"></p>';
echo '<p>' . __( 'No activity yet!' ) . '</p>';
echo '</div>';
}
echo '</div>';
}
We might have used instead:
// No comments!
$recent_comments = wp_dashboard_recent_comments( $total_items = 0 );
Approach #2
Another shorter approach would be to hook into the dashboard_recent_posts_query_args
filter on the dashboard page and remove the comments:
/**
* Remove the latest comments from the Activity Dashboard widget (approach #2)
*/
add_action( 'load-index.php', function(){
add_filter( 'dashboard_recent_posts_query_args', function( $args )
{
add_filter( 'the_comments', '__return_false' );
return $args;
} );
} );
Approach #3
We could then add further restrictions, like only empty the comments once:
/**
* Remove the latest comments from the Activity Dashboard widget (approach #3)
*/
add_action( 'load-index.php', function(){
add_filter( 'dashboard_recent_posts_query_args', function( $args )
{
add_filter( 'the_comments', function( $comments )
{
static $instance = 0;
$comments = ( 1 === ++$instance ) ? false : $comments;
return $comments;
} );
return $args;
} );
} );
Approach #4
If we want to reduce the comments database query, then we could use the pre_get_comments
hook instead.
Here's one such idea:
/**
* Remove the latest comments from the Activity Dashboard widget (approach #4)
*/
add_action( 'load-index.php', function()
{
add_filter( 'dashboard_recent_posts_query_args', function( $args )
{
// Let's exit the WP_Comment_Query from the get_comments_ids() method:
add_action( 'pre_get_comments', function( \WP_Comment_Query $q )
{
if( 1 === did_action( 'pre_get_comments' ) )
$q->set( 'count', true );
// Override the WHERE part
add_filter( 'comments_clauses', function( $clauses )
{
if( 1 === did_action( 'pre_get_comments' ) )
$clauses['where'] = ' 0=1 ';
return $clauses;
}, PHP_INT_MAX );
} );
return $args;
} );
} );
where we use the count
query variable of WP_Comment_Query
to minimize the database query.
Approach #5
Here's even a more drastic approach:
/**
* Remove latest comments from the Activity Dashboard widget (approach #5)
*/
add_action( 'load-index.php', function()
{
add_filter( 'dashboard_recent_posts_query_args', function( $args )
{
// Let's exit the WP_Comment_Query from the get_comments_ids() method:
add_action( 'pre_get_comments', function( \WP_Comment_Query $q )
{
if( 1 === did_action( 'pre_get_comments' ) )
$q->set( 'count', true );
// Eliminate the next query
add_filter( 'query', function( $query )
{
static $instance = 0;
if( 1 === ++$instance )
$query = false;
return $query;
}, PHP_INT_MAX );
} );
return $args;
} );
} );
where we just eliminate that comment query.
Approach #6
Finally here's the CSS version:
/**
* Hide the latest comments from the Activity Dashboard widget (approach #6)
*/
add_action( 'admin_head-index.php', function()
{
print '<style>#latest-comments{ display:none; }</style>';
} );