0

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(); ?>
0

1 Answer 1

2

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>';
}
?>
2
  • Thank you very much for your attention. I am so sorry. in my question . I mean author's posts comments . these comments written by readers of post yesterday
  • @TeymurAbbasov, I'm sorry for the misunderstandment. To get the comments from posts where the user is the post author, simply swap the user_id parameter to post_author. yesterday

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.