Hi Loc,
Some reading for you, let me know if you found what you were looking for:
http://wp-api.org/
Querying All Children of Parent Post using WP-API
Pascal.
Hi Pascal, thanks the these links. However those addressed the problem seen in WP REST Api version 1 and version 2 is the refactor code for WP REST API which will eventually go into WordPress Core. Version 2 is what I’m using and it doesn’t work as explain in those blog.
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;
}