1

I'm trying to display a list of the author comments in the authors page but it only shows No comments made.

How can I list user comments in the authors page?

<?php
$args = array(
    'user_id' => $user->ID,
    'number' => 10, // how many comments to retrieve
    'status' => 'approve'
    );

$comments = get_comments( $args );

if ( $comments )
{
    $output.= "<ul>\n";
    foreach ( $comments as $c )
    {
    $output.= '<li>';
    $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
    $output.= get_the_title($c->comment_post_ID);
    $output.= '</a>, Posted on: '. mysql2date('m/d/Y', $c->comment_date, $translate);
    $output.= "</li>\n";
    }
    $output.= '</ul>';

    echo $output;
} else { echo "No comments made";} ?>
  • Is $user->ID set ? What happens if you remove the 'user_id' part? I assume you got plenty of approved comments. – birgire Dec 20 '15 at 19:12
1

Try with WP_Comment_Query and make sure you have the right Author ID from the Author Template.

// WP_Comment_Query arguments
$args = array (
    'user_id'        => $user->ID,
    'post_status'    => 'approve',
    'number'         => '10',
);

// The Comment Query
$comments = new WP_Comment_Query;
$comments = $comments->query( $args );

// The Comment Loop
if ( $comments ) {

    $output.= "<ul>\n";

    foreach ( $comments as $c ) {
        $output.= '<li>';
        $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
        $output.= get_the_title($c->comment_post_ID);
        $output.= '</a>, Posted on: '. mysql2date('m/d/Y', $c->comment_date, $translate);
        $output.= "</li>\n";
    }

    $output.= '</ul>';

    echo $output;
} else {
    echo 'No comments found.';
}

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.