. . .
my $query = qq(
with recursive cte (comment_ID, comment_post_ID, comment_author, comment_parent,
comment_date_gmt, comment_type, comment_content) as (
select comment_ID,
comment_post_ID,
comment_author,
comment_parent,
comment_date_gmt,
comment_type,
comment_content
from wp_comments
where comment_ID = ? AND comment_approved = 1
union all
select p.comment_ID,
p.comment_post_ID,
p.comment_author,
p.comment_parent,
p.comment_date_gmt,
p.comment_type,
p.comment_content
from wp_comments p
inner join cte
on p.comment_parent = cte.comment_ID
)
SELECT *
FROM cte ORDER BY comment_date_gmt;
);
my $sth = $dbh->prepare($query);
$sth->execute($id);
while(my $row = $sth->fetchrow_hashref) {
my $cid = $row->{comment_ID};
my $parent_id = $row->{comment_parent};
my $post_id = $row->{comment_post_ID};
if ($parent_id eq 0) {
push(@{$posts{$post_id}}, $cid);
}
$comments{$cid}->{comment_post_ID} = $row->{comment_post_ID};
$comments{$cid}->{comment_parent} = $row->{comment_parent};
$comments{$cid}->{comment_author} = $row->{comment_author};
$comments{$cid}->{comment_date_gmt} = $row->{comment_date_gmt};
my $content = $row->{comment_content};
$content =~ s|(\s*)\n(\s*)\n|$1
\n$2
\n|gm;
$comments{$cid}->{comment_content} = $content;
push (@{$hierarchy{$parent_id}}, $cid);
}
$sth->finish();
. . .