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

Michael D Adams mda at blogwaffe.com
Mon Sep 19 19:20:22 UTC 2011


On Mon, Sep 19, 2011 at 11:43 AM, Michael D Adams <mda at blogwaffe.com> wrote:
> 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

Oh - sorry.  I completely misread your question :)

As Pau Amma mentions, it's unclear how you're defining "thread".  In
any case, you may be able to use WP's Walker classes for this, or you
could just write your own recursion in PHP.

Where are you doing this?  If you're doing it in a theme, the
$wp_query->comments array is probably already populated, which means
the comments should all be cached.  So doing individual get_comment(
$comment_ID ) calls won't need to access the DB.


More information about the wp-hackers mailing list