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?

Read More

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).

Related posts

Leave a Reply

3 comments

  1. 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).

  2. Totally untested, but you should be able to use the pre_get_comments filter:

     function exclude_admin_comments($query) {
        $query->query_vars['user_id'] != 1;
      }
    
      add_action('pre_get_comments', 'exclude_admin_comments');
    
  3. Note –

    It uses the values stored in global variable – wp_query. Usually the wp_query variable contains comments of current post when browsing.

    Usage –

    $comments = wpse61072_hide_admin_comment(5); // 5=max comments
    foreach  ( $comments as $comment ) {
        echo '<li>'.$comment->comment_author.' - said : '.$comment->comment_content.'</li>';
        
    }
    

    Functions.php –

    /*
     * Usage : $comments = wpse61072_hide_admin_comment($post->ID, 5);      
     * Show Count : echo count(wpse61072_hide_admin_comment($post->ID, 5));     
     
     * This goes into functions.php 
    */
    function wpse61072_hide_admin_comment ( $post_id, $no ) {
        global $wp_query; // uses global variable
        $woa_comments = $wp_query->comments;
        
        //thanks @kaiser for loop
        foreach ( $woa_comments as $woa_comment => $woac ) {    
            $author = get_userdata( $woac->user_id );
            if (user_can( $author, 'manage_options' )) {    
                unset( $woa_comments[ $woa_comment ] );
            }
        }
        
        //make sure we're returing only specified elements
        return (array_slice($woa_comments,0,$no));          
    }
    

    – Tested on wordpress 3.4.1 with TwentyTen theme.