0

I have added a meta value to the comments-meta table that adds a custom 'status' type code (called 'redacted') to a comment. I have used the following code to display a link to display only those comments with a specific meta value on the edit-comments.php admin Comments page. This was done with the following code:

// add to the comment links at the top of the comments list admin page
    function my_comment_link_add($status_links) {
        $status_links['redacted']= "<a href='?status=redacted'>Redacted</a>";
        return $status_links;
        }
    add_filter("comment_status_links","my_comment_link_add");

// if clicked on the link, modify the comment query
if (isset($_GET['status']) AND ($_GET['status'] == 'Redacted')) {
    add_action('pre_get_comments', 'my_comment_redacted_list' );
}
function my_comment_redacted_list($query)
{
    global $pagenow;
   // if ( is_admin() && ('your-custom-page' === $pagenow) ) {
        $query->query_vars['meta_query'] = [
                    'relation' => 'AND',
            [
                'key' => 'redact',
                'value' => '1'
            ],
        ];
     
        return;     }

Clicking on the new 'Redacted' link displays the comments that have the 'redact' key in the comment_meta table.

But:

  1. the 'Redacted' link does not show the count of the custom query records found (when All comments are shown in the table).
  2. If I click on the 'Redacted' link, only the 'redacted' comments are shown on the list. But the 'All' link only shows the count of the custom query (the count of 'redacted' comments), not the total number of comments. (In fact, all of the counters on the links line only show the result of the custom query.)

Is there a more efficient way of doing this? How do I show the proper count for the 'Redacted' link on the 'all comments' table list. And how do I show the proper 'All' count when only the 'Redacted' comments are shown on the table list.

1 Answer 1

0

To show the proper count for the 'Redacted' link on the 'all comments' table list, you can use the get_comments() function to get the total number of 'redacted' comments and then update the link text to include this count. Here is an example of how you can modify the my_comment_link_add() function to do this:

function my_comment_link_add($status_links) {
    // Get the total number of redacted comments
    $args = array(
        'meta_key' => 'redact',
        'meta_value' => '1',
        'count' => true
    );
    $redacted_count = get_comments($args);

    // Update the link text to include the count
    $status_links['redacted']= "<a href='?status=redacted'>Redacted ($redacted_count)</a>";
    return $status_links;
}
add_filter("comment_status_links","my_comment_link_add");

To show the proper 'All' count when only the 'Redacted' comments are shown on the table list, you can use the get_comments_count() function to get the total number of comments and then update the link text to include this count. Here is an example of how you can modify the my_comment_redacted_list() function to do this:

function my_comment_redacted_list($query) {
    global $pagenow;
    // Get the total number of comments
    $all_count = get_comments_count();
    //

Lemme know if this helps.

1
  • Your first code block works OK, it returns the number of redacted comments. The second block doesn't work: get_comment_count() (you have typo in yours) returns the count of the current query, not all site comments. Commented Jan 3, 2023 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.