Reply To: Reply Walker output to JSON format
I registered a new rest endpoint and wrote functions to retrieve all the replies with the code below. Although the code works, I’m not really happy with it because I’m not able utilize built in filtering (i.e. paged, order, posts_per_page… etc). This code is not optimize because it has no pagination and will retrieve all replies at once (can be bad if there are thousands of records).
function ldp_get_topic_replies( $data ) {
$topicID = $data['id'];
// Get an array of all replies belonging to a specific topic
$repliesID = bbp_get_all_child_ids($topicID, bbp_get_reply_post_type());
$response = array();
foreach ($repliesID as $replyID) {
$response[] = ldp_get_topic_reply($replyID);
}
return $response;
}
function ldp_get_topic_reply( $replyID ) {
$reply = array(
'author_name' => bbp_get_reply_author_display_name($replyID),
'author_email' => bbp_get_reply_author_email($replyID),
'author_url' => bbp_get_reply_author_url($replyID),
'author_avatar' => get_avatar_url(bbp_get_reply_author_id($replyID)),
'link' => bbp_get_reply_url($replyID),
'reply_to' => bbp_get_reply_to( $replyID ) == 0 ? NULL : bbp_get_reply_to( $replyID ),
'topic_title' => bbp_get_reply_topic_title($replyID),
'meta' => bbp_get_reply($replyID),
);
return $reply;
}