I am trying to display authors comments on their profile page (author.php), however both codes I tried seem to display everyone’s comments. Also the second code is suppose to link to the specific comment, but instead it does nothing. Also the comments id is already added to the comments output and it prints fine. Any help is greatly appreciated.

// Method 1
<ul class="authpcom">
<?php
$author_email = get_the_author_meta( 'user_email' ); 

$args = array(
    'author_email' => $author_email
);
$comments = get_comments($args);
foreach($comments as $comment) :
    echo('<a href="https://wordpress.stackexchange.com/questions/199860/" . get_permalink($comment->post_ID) . "https://wordpress.stackexchange.com/questions/199860/" rel="external nofollow" title="https://wordpress.stackexchange.com/questions/199860/" . $title . "https://wordpress.stackexchange.com/questions/199860/">' .$title . '</a><br />' . $comment->comment_date . '<br /><li>' . $comment->comment_content . '</li>');
endforeach;
?>
</ul>


// Method 2
<?php   $comments = get_comments(); ?>
<ul id="recent_comments">
<?php foreach ($comments as $comment) { ?>
<li><p><strong><?php
        $title = get_the_title($comment->comment_post_ID);
        echo get_avatar( $comment, '45' );
echo strip_tags($comment->comment_author); ?></strong>&nbsp;commented on <a href="<?php echo get_permalink($comment->comment_post_ID); ?>#comment-<?php echo $comment->comment_ID; ?>" rel="external nofollow" title="<?php echo $title; ?>"> <?php echo $title; ?></a>: <?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?> (...)</p></li>
<?php }  ?>
</ul>

Used on comment output divs –

$comment->comment_ID

object(WP_User)#345 (7) { [“data”]=> object(stdClass)#344 (10) {
[“ID”]=> string(1) “2” [“user_login”]=> string(6) “agent1”
[“user_pass”]=> string(34) “$P$BXUSPFSBfmyIrjZ2YUnbIs1GwjkdH50”
[“user_nicename”]=> string(6) “agent1” [“user_email”]=> string(19)
“agent1@homekast.com” [“user_url”]=> string(0) “”
[“user_registered”]=> string(19) “2015-07-25 10:33:27”
[“user_activation_key”]=> string(0) “” [“user_status”]=> string(1) “0”
[“display_name”]=> string(9) “John Paul” } [“ID”]=> int(2) [“caps”]=>
array(1) { [“agent”]=> bool(true) } [“cap_key”]=> string(15)
“tr_capabilities” [“roles”]=> array(1) { [0]=> string(5) “agent” }
[“allcaps”]=> array(2) { [“read”]=> bool(true) [“agent”]=> bool(true)
} [“filter”]=> NULL }

3 s
3

What you need to use here is the WP_Comment_Query() function.

So on the author.php page, you can easily get the author info and ID as followed:

// get author info
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

// set ID
$user_id = $curauth->ID;

Then we add the user ID in the query arguments array:

$args = array(
    'user_id' => $user_id, // comments by this user only
    'status' => 'approve',
    'post_status' => 'publish',
    'post_type' => 'post'
);

And finally we hit the arguments in the wp_comment_query():

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

// Comment Loop
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo '<p>' . $comment->comment_content . '</p>';
    }
} else {
    echo 'No comments found.';
}

As an added bonus, I investigated how pagination works with wp_comment_query() for not long ago and offer a good solution here. It was a little bit of a fiddle-niddle to make it work.

EDIT:

A better way to get the author ID is simply with (props @Pieter):

$user_id = get_queried_object_id();

Leave a Reply

Your email address will not be published. Required fields are marked *