-1

I want to get all child comments ids from parent comment id. Do i have to write custom sql or can i use get_comments() to achieve the following output.

eg 1

I want to get 2,3,4,5,6,7,8 ids from parent id 1.

|- 1 (parent comment)
|-- 2 ( child of 1)
|--- 3 ( grand child )
|-- 4 ( child of 1)
|--- 5 ( grand child )
|---- 6 ( grand grand child )
|----- 7 ( grand grand grand child )
|------ 8 ( grand grand grand grand child )

eg 2

I want to get 5,6,7,8 comment ids from parent id 4.

|- 1 (parent comment)
|-- 2 ( child of 1)
|--- 3 ( grand child )
|-- 4 ( child of 1)
|--- 5 ( grand child )
|---- 6 ( grand grand child )
|----- 7 ( grand grand grand child )
|------ 8 ( grand grand grand grand child )
  • Hi guys no problem i solved it myself – Ravi Shakya Oct 11 '15 at 15:22
  • 2
    Please add your solution as an answer so that others can benefit and learn from it. :) – Rarst Oct 11 '15 at 17:35
  • Why not deleting this kind of questions (with a (kinda empty) comment as answer, which leaves other users still stare into the dark) – Charles Feb 27 '16 at 2:19
2

For anyone else trying to find this:

This can be done using the $parent parameter of get_comments.

Here is the code assuming $parent_comment_id is the id of the parent comment.

$childcomments = get_comments(array(
    'post_id'   => get_the_ID(),
    'status'    => 'approve',
    'order'     => 'DESC',
    'parent'    => $question->comment_ID,
));

Now $childcomments will have an object with comment_ID as the ID of the comment. Refer to the 'Returns' of get_comment for details.

Hope this helps someone else searching for this.

2

I found this post because I was looking for a similar solution.

As suggested by Captain47, get_comments with parent args, won't work because it's return only 1 level nested comments.

To get unlimited nested you need to use hierarchical arg as:

           $args = array(
            'parent' => $comment_ID,
            'hierarchical' => true,
           );
        $questions = get_comments($args);

Reference: https://codex.wordpress.org/Class_Reference/WP_Comment_Query

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.