WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

share|improve this question
You can use the action pre_get_comments to filter out the admin comments. – Rutwick Gangurde Aug 7 '12 at 6:05

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');
share|improve this answer
:( Did anything here, placed right before the function in functions.php. Thanks anyway. – Diana Aug 7 '12 at 19:00
If I'm not wrong, the pre_get_comments() hook is used to change the args supplied to get_comments() functions. – amit Aug 8 '12 at 8:50

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

share|improve this answer
I'm sorry, your plugin is not working here :( is still displaying admin comments wherever I use get_comments. I just want display latest comments in some areas then I created a function in function.php(not a plugin, just a theme). – Diana Aug 8 '12 at 1:47
I think there's type - unset( $comments[ $index ]; ) – amit Aug 8 '12 at 7:22
@amit Many many thanks! :) I just didn't see it and currently have no dev env, where I got comments. Just a sidenote: You can always fell free to go into my answers and edit them (like you do with all those questions ;) ). – kaiser Aug 8 '12 at 11:44
@Diana Typo fixed (props amit) - give it a try and report back please. – kaiser Aug 8 '12 at 11:44
Still no luck :( Is not working. I get syntax error, unexpected T_OBJECT_OPERATOR, expecting ')' in line 'function wpse_61072_comments_without_admin( $comments, $post->ID )'. I removed $post->ID, no errors but admin comments are stil showing. Why use $post->ID? – Diana Aug 8 '12 at 18:35

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.

share|improve this answer
This would work if you don't mind the number of comments being inconsistant. ie: your pulling 5 comments before filtering, if admin is the author of 2, you'll end up only displaying 3 comments. – FlashingCursor Aug 7 '12 at 6:54
You are right. I didn't think about that. – amit Aug 7 '12 at 7:05
Yes, works but something I get 0 comments then :D thanks anyway – Diana Aug 7 '12 at 19:06
Updated answer with new example, uses - foreach loop by @kaiser. – amit Aug 8 '12 at 23:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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