1

Is there any way to filter the comments that are In Reply from the comments list? I basically just want to see the depth 1 comments on a separate filter.

@GhostToast Ive added this to create a new filter 'Ask' on the Admin Comments Panel. is there a way to just apply your top level comments filter code to this Ask filter? That would be really good because the All comments filter still works and the Ask is filtered by showing only the top level ones...


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['ask'] ) )
        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'] = 12;
}

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

    if( isset( $_GET['ask'] ) ) 
    {
        $status_links['all'] = 'All';
        $status_links['ask'] = 'Ask ('.$count.')';
    } 
    else 
    {
        $status_links['ask'] = 'Ask ('.$count.')';
    }

    return $status_links;
}
1
  • sure, updating answer – GhostToast Jun 4 '13 at 15:14
1

In your comments loop, skip the children:

if( !empty( $comment->comment_parent ) ) { 
    //if the comment has a parent, skip it as it's not level 1
    continue;
}

Edit: Above example would only help in the reading loop, below example should help in admin area:

add_filter('the_comments', 'top_level_comments_filter');

function top_level_comments_filter($comments){
    global $pagenow;
    if($pagenow == 'edit-comments.php'){
        foreach($comments as $key => $value){
            if( !empty( $value->comment_parent ) ) { 
                unset($comments[$key]);
                continue;
            }
        }
    }
    return $comments;
}

Edit: Per your addendum to the original question, this would be how you put it all together. I've cleaned up your code a little and given the functions a cohesive name-space. This lumps the filter I gave you in with the condition of the ask query var being set:

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

/**
 * Delay hooking our clauses filter to ensure it's only applied when needed.
 */
function dreis_comments_exclude_lazy_hook( $screen ) {
    if ( $screen->id != 'edit-comments' ) {
        return;
    }

    // Check if our Query Var is defined    
    if( isset( $_GET['ask'] ) ) {
        add_action( 'pre_get_comments', 'dreis_list_comments_from_specific_post', 10, 1 );
        add_filter( 'the_comments', 'dreis_top_level_comments_filter' );
    }

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

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

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

    if( isset( $_GET['ask'] ) ) {
        $status_links['all'] = 'All';
        $status_links['ask'] = 'Ask ('.$count.')';
    } else {
        $status_links['ask'] = 'Ask ('.$count.')';
    }

    return $status_links;
}

/**
 * Remove non-top-level comments
 */
function dreis_top_level_comments_filter($comments){
    global $pagenow;
    if($pagenow == 'edit-comments.php'){
        foreach($comments as $key => $value){
            if( !empty( $value->comment_parent ) ) { 
                unset($comments[$key]);
            continue;
            }
        }
    }
    return $comments;
}
4
  • Thanks for that @GhostToast . but that is to filter on the front end right? what i need is to filter on the back end, on the commments admin panel... – David Reis Jun 4 '13 at 14:11
  • oops. added admin example. you'd need a way to turn it on and off though. – GhostToast Jun 4 '13 at 14:16
  • wow great stuff @GhostToast. Almost there just need a way to turn this on and off. is there an easy way? – David Reis Jun 4 '13 at 14:26
  • A $_GET variable comes to mind. Could be an extra parameter in the edit-comments.php URL string... You could then create a bookmark to that page. Kind of ghetto, but pretty easy! Like: url.com/wp-admin/edit-comments.php?secret_filter_mode=on and then look for that variable to be assigned before filtering? – GhostToast Jun 4 '13 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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