Get all comments of author's posts

Hello I have multi author website. I want to display all comments of posts created by author in author page. Please help me. I found this code but fatal error

?php
    $args = array(
        'author' = AUTHOR_ID,
        'posts_per_page' = 500, //Don't use -1 here
    );
    $the_query = new WP_Query( $args );
    if($the_query-have_posts() ) : ?
        ?php while ( $the_query-have_posts() ) : ?
            ?php
            $nested_args = array(
                'post_id' = get_the_ID()
            );
            $comments_query = new WP_Comment_Query;
            $comments = $comments_query-query( $nested_args );
            if ( $comments ) {
                foreach ( $comments as $comment ) {
                    echo 'p' . $comment-comment_content . '/p';
                }
            }
        ?php endwhile; ?
    ?php endif; ?
?php wp_reset_query(); ?

Topic author comments query posts Wordpress

Category Web


It looks like there's two problem with the code you're trying to use. The first is AUTHOR_ID. Unless you have this defined somewhere else, then this is going to cause an undefined constant error. The second one is that there should be a closing PHP tag ?> before <?php endwhile; ?>.

Another thing is, that doing WP_Query is a bit unnecessary as you can pass the user ID as a parameter to the WP_Comment_Query directly.

For example on author.php template you could do this,

<?php
$query = new WP_Comment_Query( array(
    'user_id' => get_queried_object_id()
) );

foreach ($query->comments as $comment) {
    echo '<p>' . $comment->comment_content . '</p>';
}
?>

EDIT

To get comments from the author's posts, use post_author parameter.

<?php
$query = new WP_Comment_Query( array(
    'post_author' => get_queried_object_id()
) );

foreach ($query->comments as $comment) {
    echo '<p>' . $comment->comment_content . '</p>';
}
?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.