Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created September 23, 2010 20:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikeschinkel/594346 to your computer and use it in GitHub Desktop.
Save mikeschinkel/594346 to your computer and use it in GitHub Desktop.
get_comment_replies() function for WordPress with a working example that returns a WordPress comment object for a given comment ID annotated with "reply_count" and "replies" properties.
<?php
/*
* test-get-comment-replies.php
*
* Returns a comment object for a given comment ID annotated with "reply_count" and "replies" properties:
*
* reply_count: count of all replies including sum of child replies.
* replies: array of child comment objects also annotated with "reply_count" and "replies" properties, recursively.
*
* Examples:
*
* // Get comment with nested replies for comment ID = 55
* $comment_with_replies = get_comment_replies(55);
*
* // Get all comments with nested replies for post ID = 17
* $comment_with_replies = get_comment_replies(0,array('post_id'=>17));
*
* // Get ALL comments with replies
* $comment_with_replies = get_comment_replies('all');
*
* In response to: http://lists.automattic.com/pipermail/wp-hackers/2010-September/035086.html
*
* Save this example test file in the website root as /est-get-comment-replies.php and call it like so:
*
* http://example.com/est-get-comment-replies.php?comment_id=123
*
* By Mike Schinkel
*
*/
header('Content-type:text/plain');
include "wp-load.php";
$args = array();
if (isset($_GET['post_id']))
$args['post_id'] = $_GET['post_id'];
$replies = get_comment_replies($_GET['comment_id'],$args);
print_r($replies);
function get_comment_replies($comment_id=0,$args=array()) {
$args = wp_parse_args($args,array(
'post_id'=>0,
'orderby'=>'comment_parent',
'order'=>'ASC',
));
extract($args);
if (is_numeric($comment_id) && $comment_id>0 && !$post_id) {
$comment = get_comment($comment_id);
$post_id = $comment->comment_post_ID;
}
if (!$post_id && !is_numeric($comment_id) && $comment_id!='all') {
wp_die('get_comment_replies() will not parse all comments unless explicitly requested.');
}
$comments = get_comments($args);
$replies = array();
foreach($comments as $comment) {
$comment->reply_count = 0;
$comment->replies = array();
$replies[$comment->comment_ID] = $comment;
if ($comment_id==$comment->comment_ID)
$this_comment = $comment;
if ($comment->comment_parent) {
if (!isset($replies[$comment->comment_parent]->replies))
$replies[$comment->comment_parent]->replies = array();
$replies[$comment->comment_parent]->replies[$comment->comment_ID] = $comment;
}
}
$children = array();
$count = 0;
if ($comment_id>0) {
$this_comment->reply_count = _count_comment_replies($replies,$comment_id);
$this_comment->replies = $replies[$comment_id]->replies;
$replies = $this_comment;
}
return $replies;
}
function _count_comment_replies(&$replies,$comment_id) {
$count = 0;
if (count($replies[$comment_id]->replies)>0) {
$count = count($replies[$comment_id]->replies);
foreach($replies[$comment_id]->replies as $index => $reply) {
$count += _count_comment_replies($replies,$reply->comment_ID);
}
$replies[$comment_id]->reply_count = $count;
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment