5

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=" ' . get_permalink($comment->post_ID) . ' " rel="external nofollow" title=" ' . $title . ' ">' .$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 }

5
+25

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();
  • You can get the author ID with get_queried_object_id() on author pages :-) – Pieter Goosen Sep 1 '15 at 16:52
  • Also, get_posts are in some instances easier to use than WP_Query as it already skips/exclude pagination and sticky posts and also ignore the posts_* filters :-) – Pieter Goosen Sep 1 '15 at 16:54
  • @PieterGoosen Removed my statement about get_posts() to avoid confusion and added a note about get_queried_object_id() -- very nice! – Christine Cooper Sep 1 '15 at 17:02
  • I placed the code in author.php and it showed no comments at all. only the no comments found text. – Rich Sep 2 '15 at 2:37
  • Confirm if $user_id is set. Run echo $user_id; and see if it displays the user id of the user who you like to loop comments. Also confirm if this very user has posted any comments. – Christine Cooper Sep 2 '15 at 15:16
1

First method, you are missing the second parameter for get_the_author_meta, which is the ID of the author.

Second method, you are using undefined variables.. Check this code, should get you what you want.

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

        $args = array(
            'author_email' => $author_email
        );
        $comments = get_comments($args);
        foreach($comments as $comment) :
            echo '<a href=" ' . get_permalink( $comment->comment_post_ID ) . ' " rel="external nofollow" title=" ' . get_the_title( $comment->comment_post_ID ) . ' ">' . get_the_title( $comment->comment_post_ID ) . '</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
            echo get_avatar( $comment->comment_author_email, '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 get_the_title( $comment->comment_post_ID ); ?>"> <?php echo get_the_title( $comment->comment_post_ID ); ?></a>: <?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?> (...)
    </p>
</li>
<?php }  ?>
</ul>

EDIT: (09/05/2015)

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

        $args = array(
            'user_id' => $authorID,
        );
        $comments = get_comments($args);
        if ( $comments ) {
            foreach($comments as $comment) {
                echo '<li><a href="' . get_permalink( $comment->comment_post_ID ) . '" rel="external nofollow" title="' . get_the_title( $comment->comment_post_ID ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a><br>' . $comment->comment_date . '<br>' . $comment->comment_content . '</li>';
            }
        } else {
            echo '<li>No Comments from this Author</li>';
        }
    ?>
</ul>
  • thanks for the reply however, the first method doesnt do anything and the second one does the same. It prints out all the comments made by everyone. This is on author.php. – Rich Aug 31 '15 at 3:57
  • @mygm26 the second method should display all the comments, regardless of the comment author as you are not passing any filter to get comment from any particular. However, the first method SHOULD work. Try the new code, I have added a check to see if it can find any existing comment from the author. – Abhik Aug 31 '15 at 5:33
  • Still tells me no comments @Abhik – Rich Sep 4 '15 at 6:32
  • @mygm26 Can you post a var_dump of get_queried_object() of author.php? function obj_check() { $queried_object = get_queried_object(); var_dump( $queried_object ); } add_action( 'wp_footer', 'obj_check' ); Load any author profile page after adding this code and put the output in your original question? – Abhik Sep 4 '15 at 18:37
  • ok will update now – Rich Sep 4 '15 at 20:09
0

You can achieve this using the ultimate member plugin. It seems to be able to do a lot of things with user profiles, but also appears to be suitable to display user's comments on the profile page.

Show author posts & comments on user profiles

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.