[wp-hackers] Retrieving all the comments in a thread

Michael D Adams mda at blogwaffe.com
Mon Sep 19 18:43:32 UTC 2011


On Mon, Sep 19, 2011 at 11:21 AM, Dino Termini <dino at duechiacchiere.it> wrote:
> I was wondering if Wordpress has a built-in method to get all the comments
> in a given thread. Let's say I have

WordPress.

> - post
> -- comment 1 [ID = 1]
> --- reply 1 to comment 1 [ID = 2]
> --- reply 2 to comment 1 [ID = 3]
> --- reply 3 to comment 1 [ID = 4]
> -- comment 2 [ID = 5]
> -- comment 3 [ID = 6]
> --- reply 1 to comment 3 [ID = 7]
> --- reply 2 to comment 3 [ID = 8]
> ---- reply 1 to reply 2 to comment 1 [ID = 9]
> ---- reply 2 to reply 2 to comment 1 [ID = 10]
>
> Given any comment ID, I'd like to get all the comment IDs in the same
> thread. So for ID = 3, I'd get {1, 2, 3, 4}, and so on. How would you
> accomplish something like this in Wordpress, since the only available
> relationship between comments is parent/son and there's no recursion in
> Mysql?

Check out get_comments() - you can do something like:

get_comments( array( 'post_id' => 123 ) );

to get all the comments of that post.

http://codex.wordpress.org/Function%20Reference/get_comments


More information about the wp-hackers mailing list