How to get last comments but not from admin (or other specific user role/capability)?

I’m using get_comments to show lastest comments i.e.:

<?php
    global $comments, $comment;
    $comments = get_comments( array( 'number' => '5', 'status' => 'approve', 'post_status' => 'publish' ) );

    foreach ( (array) $comments as $comment) {
     echo '<li>'.$comment->comment_author.' said '.$comment->comment_content .'</li>'
    }
;?>

Is there some way to override admin comments?! I know this tip using SQL but maybe Im’missing something in this function?

Thanks for help.

Updated code
Place this code wherever template file such sidebar.php, footer.php and will list latest comments (displaying Admin comments tough).

3 Answers
3

Yay! We got filters!

Wrapping the result of the $wpdb comments query right into a filter callback (and a plugin) is the nice way of handling comments.

/* Plugin Name: »Kaisers« Comments without admin comments */
! defined( 'ABSPATH' ) AND exit;

add_filter( 'comments_array', 'wpse_61072_comments_without_admin', 20, 2 );
function wpse_61072_comments_without_admin( $comments, $post_id )
{
    foreach ( (array) $comments as $index => $c )
    {
        // Add a limit
        if ( 5 <= absint( $index + 1 ) )
            return (object) $comments;

        // Get the authors user data
        $author = get_userdata( $c->user_id );
        // Unset based on the standard/default capability
        if ( user_can( $author->ID, 'manage_options' ) )
            unset( $comments[ $index ] );
    }

    return (object) $comments;
}

This one should work (not tested).

Leave a Comment