Skip to:
Content
Pages
Categories
Search
Top
Bottom

Function to return all replies to a topic

  • @saciojote

    Participant

    Is there a function that returns all the replies when the topicID is passed into a function?

    Some example code is below for better understanding of the function I am asking about –

    
    $args = array(
    	'post_parent' => $NUM, // ID of the topic
    	'order'       => 'ASC', // Order of the replies (ASC or DESC)
    	);
    
    $replies = bbp_get_replies_function( $args ); // function i would like
    $response = array(
          	status' => 'success',
          	'message' => $replies,
        	);
    
Viewing 13 replies - 1 through 13 (of 13 total)
  • @robin-w

    Moderator

    yes

    $replies = bbp_has_replies( $args );

    in \bbpress\includes\replies\template.php

    it has these default args (some set by code above this section)

    $default = array(
    		'post_type'              => $default_post_type,         // Only replies
    		'post_parent'            => $default_post_parent,       // Of this topic
    		'posts_per_page'         => bbp_get_replies_per_page(), // This many
    		'paged'                  => bbp_get_paged(),            // On this page
    		'orderby'                => 'date',                     // Sorted by date
    		'order'                  => 'ASC',                      // Oldest to newest
    		'hierarchical'           => $default_thread_replies,    // Hierarchical replies
    		'ignore_sticky_posts'    => true,                       // Stickies not supported
    		'update_post_term_cache' => false,                      // No terms to cache

    but whatever you pass in $args will overwrite these

    @saciojote

    Participant

    Thank you!

    @robin-w

    Moderator

    🙂

    @saciojote

    Participant

    Follow up from this question, is there a function which you pass the title of a topic “ABC” and it returns back the id of said topic i.e 123?

    @robin-w

    Moderator

    titles can be duplicated – eg you can have 2 topics called say ‘help’

    the title will be the same, but the permalinks will be

    forums/topic/help/
    and
    forums/topic/help-2/

    so there is no title to id function

    @saciojote

    Participant

    Okay I understand thank you.

    So is there no way in returning the topicID when I just have the title?

    @robin-w

    Moderator

    only if you have one topic with that title, and since you cannot control what users title their topics this cannot be guaranteed.

    You could return an array of topic ID’s that match the title, but not sure how much that would help.

    what are you trying to achieve?

    @saciojote

    Participant

    Yes, I it is guaranteed that the topic title is unique – I am the only one who creates the topics.

    I am trying to link my site with an app, and I have implemented a search bar so when the user searches a topic, then all the replies to that topic will show.

    I have everything working except returning the replies!

    … The array of topic IDs sounds like it would work, how would i do that?

    @robin-w

    Moderator

    if title is unique then

    get_page_by_title()

    untested but something like :

    function rew_find_topic_id ($title) {
    	$topic = get_page_by_title($title, OBJECT, bbp_get_topic_post_type());
    	if (!empty ($topic) {
    		$topic_id = $topic->ID ;
    		return $topic_id ;
    	}
    	else {
    		return 'empty' ;
    	}
    }

    @saciojote

    Participant

    Hi Robin, thank you for the example code! I have tried it and it seems to return the same value regardless of the title, so I am unsure…

    Here is the sample code that I have used the function within:

    function return_content_replies() {
    	
    	$title = $_POST['title'];
    	$topic_id = rew_find_topic_id($title);
    	
    	if ( $topic_id == 'empty' ) { 
    	  	// create the topic here
    	} else {
    		$args = array(
    			  'post_parent' => $topic_id, // ID of the topic
    			  'order'       => 'ASC', // Order of the replies (ASC or DESC)
    			);
    			
    			$replies_array = array();
    		
    			if ( bbp_has_replies( $args ) ) {
    			  while ( bbp_replies() ) {
    				bbp_the_reply();
    				$replies_array[] = array(
    				  'id' => bbp_get_reply_id(),
    				  'content' => bbp_get_reply_content(),
    				  'date' => bbp_get_reply_post_date(),
    				);
    			}
    		}
    			$response = array( 'status' => 'success', 'message' => $topic_id);
    	}
    	wp_send_json($response);
    }

    @robin-w

    Moderator

    did you correct the error in this line

    if (!empty ($topic) {

    should be

    if (!empty ($topic)) {

    @saciojote

    Participant

    Yes I did I noticed that before I tried the code. I will play around and see if I can find the problem, if the get_page_by_title() works as I would like then it must be an error somewhere else.

    Thank you for your help!

    @robin-w

    Moderator

    my code above corrected works, but yes do come back and let us know if you find the problem or need further help

Viewing 13 replies - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.
Skip to toolbar