Is there a way to get replies of a comment by its ID? I have searched on google but could not find anything about it.

For example, I have a comment with ID 123

I want to display all the replies that comment ID 123 has.

I have tried something like the following but it did not work:

$args = array(
    'status' => 'approve',
    'ID' => 123,
    'number' => '5',
    'post_id' => 73871,
    'comment_parent' => 0

);
$comments = get_comments($args);

I think I just found it, please corrent me if I am wrong:

$args = array(
    'status' => 'approve', 
    'number' => '5',
    'post_id' => 73871,
    'parent' => 123

);
$comments = get_comments($args);

EDIT: not quite, it seems it returns the first reply only. The rest are missing.

You're correct so far but incomplete.

You need a foreach loop after your code. So it will be like:

$args = array(
    'status' => 'approve', 
    'number' => '5',
    'post_id' => 73871,
    'parent' => 123
);

$comments = get_comments($args);

foreach ($comments as $comment) {
    echo $comment->comment_content; // echo all the other fields you need
}

As comment_ID is a unique value, there is no need to include the post_id in the arguments.

This works fine for me:

$args = array(
    'status' => 'approve', 
    'number' => '5',
    'parent' => 3194
);
$comments = get_comments($args);

This will return 5 approved comments whose parent is the comment with the comment_ID 3194.

Sample output could be done with something like this:

foreach($comments as $child_comment) {
    echo $child_comment->comment_ID;  
}

In your foreach loop, you need to search for each comment if it has any replies submitted.

$replies = get_comments( array( 'parent' => $comment->comment_ID, 'status' => 'approve', 'order' => 'ASC' ) );

please have a look at this plugin https://github.com/mukto90/comment-search/blob/master/cb-comment-search.php

it'll help you search by a comment id and show all of its replies (and maybe their replies too).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.