4

I found so many information and most of the tutorials only talk about adding filter in post/custom post only.

I want to do something similar to this tutorial in 'Comments' area in admin panel.

Add filter menu to admin list of posts (of custom type) to filter posts by custom field values

However, I'm not sure if the parse_query handle also handle the query in this area? Anyone might have suggestion on tutorial, plugin that might help?

1 Answer 1

5

Working Example [Update]

From this Answer, by @TheDeadMedic, here's an adaptation to show only comments from a specific post_id. A link to this action is inserted in the status row.

Hello World is the post with ID 53.

new comments status link

When clicked it displays only the comments of that post in the URL
example.com/wp-admin/edit-comments.php?comment_status=all&hello_world=1:

show comments of only one post

add_action( 'current_screen', 'wpse_72210_comments_exclude_lazy_hook', 10, 2 );

/**
 * Delay hooking our clauses filter to ensure it's only applied when needed.
 */
function wpse_72210_comments_exclude_lazy_hook( $screen )
{
    if ( $screen->id != 'edit-comments' )
        return;
    
    // Check if our Query Var is defined    
    if( isset( $_GET['hello_world'] ) )
        add_action( 'pre_get_comments', 'wpse_63422_list_comments_from_specific_post', 10, 1 );

    add_filter( 'comment_status_links', 'wpse_63422_new_comments_page_link' );
}

/**
 * Only display comments of specific post_id
 */ 
function wpse_63422_list_comments_from_specific_post( $clauses )
{
    $clauses->query_vars['post_id'] = 53;
}

/**
 * Add link to specific post comments with counter
 */
function wpse_63422_new_comments_page_link( $status_links )
{
    $count = get_comments( 'post_id=53&count=1' );

    if( isset( $_GET['hello_world'] ) ) 
    {
        $status_links['all'] = '<a href="edit-comments.php?comment_status=all">All</a>';
        $status_links['hello'] = '<a href="edit-comments.php?comment_status=all&hello_world=1" class="current">Hello World ('.$count.')</a>';
    } 
    else 
    {
        $status_links['hello'] = '<a href="edit-comments.php?comment_status=all&hello_world=1">Hello World ('.$count.')</a>';
    }
    
    return $status_links;
}

Useful Hooks

Search for the hooks bellow, they are available in the file /wp-admin/includes/class-wp-comments-list-table.php.

It'll give you a panorama of the possibilities for the Comments screen.

Actions

Filters

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.