Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply Walker output to JSON format

  • @phamdacloc

    Participant

    Hello,
    I’m working on an application what communicate to bbPress using RESTful API. I’m struggling to find an equivalent function that would output the nested/threaded topic replies into a data structure such as array or JSON format. I see that bbp_list_replies() does just what I wanted, but rather than putting the result inside a data structure, this function displays the output as HTML data. Is there a function which will accomplish my description above or is this something I’ll have to write up?

    Many thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • @casiepa

    Moderator

    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.

    @phamdacloc

    Participant

    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.

    @phamdacloc

    Participant

    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;
    }
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Skip to toolbar