Skip to:
Content
Pages
Categories
Search
Top
Bottom

Get total replies to a topic by the author of that topic

  • @thinlizzie

    Participant

    Hi Robin,

    I’m writing some code to pay a little “reward points” bonus to the author of a forum topic, when that topic receives X number of replies.
    All works fine, hooking to bbp_new_reply.
    But I would like to exclude any replies by the author themself from the total replies count.
    Total replies count is currently bbp_get_topic_reply_count( $topic_id )
    So I need to subtract author_own_replies from that total.

    I have $topic_id, $author_id, $reply_id, $forum_id

    Any easy way to achieve this?

    I’m trying to avoid SQL queries.

Viewing 10 replies - 1 through 10 (of 10 total)
  • @robin-w

    Moderator

    Very briefly tested as I am on holiday tomorrow, but try this

    $r = array(
    		'post_type'              =>bbp_get_reply_post_type(),   
    		'post_parent'            => $topic_id,  
    		'author__not_in' => array( $author_id)
    		);
    	$reply_posts = new WP_Query($r); 
    	$count = $reply_posts->post_count;

    @thinlizzie

    Participant

    Thanks Robin, tested, works perfectly, your help is very much appreciated.

    @robin-w

    Moderator

    Great – glad you are fixed

    @thinlizzie

    Participant

    Just a little follow up question … I’m looking at modifying my “reward” system, ignoring the number of replies and simply rewarding based on forum engagement. So a reward is triggered when a user creates a new topic or posts a reply ( capped at one reward per day, to avoid spam reward-mining )

    What should I do?

    1. Create two reward functions, hook one to bbp_new_topic and hook the other to bbp_new_reply ?
    2. Create one reward function and hook it to some kind of combo “bbp_all_user_engagement” thing, if that exists?
    3. Something different?

    Cheers!

    @markkavin

    Participant

    You can achieve this without raw SQL by looping through the replies and filtering out the author’s own ones. Something like this:

    $reply_count = 0;
    $replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() );

    if ( ! empty( $replies ) ) {
    foreach ( $replies as $reply_id ) {
    if ( get_post_field( ‘post_author’, $reply_id ) != $author_id ) {
    $reply_count++;
    }
    }
    }

    This way you count only replies not made by the topic author. Then you can compare $reply_count with your threshold to trigger the reward.

    It’s lightweight, avoids direct SQL, and keeps everything inside bbPress/WordPress functions.

    @robin-w

    Moderator

    so when a user posts a topic or they reply to a topic, you want to give them a reward immediately, but then not give them a further reward for the remainder of that day ie give them a reward for the first post they make each day.

    and not use the previous reward system

    Yes?

    @thinlizzie

    Participant

    Hi Robin, yes that’s correct. The previous system worked fine but was not really encouraging forum participation so it will be replaced.

    The daily-limit is handled by a simple flag in the usermeta table, all good.

    I’m really just asking if there exists a combo version of bbp_new_topic and bbp_new_reply for me to hook onto, or if I should simply hook onto both of those, separately.

    eg.

    
    add_action( 'bbp_new_topic' , 'my_reward_function' );
    add_action( 'bbp_new_reply' , 'my_reward_function' );
    
    function my_reward_function() {
    
    	// check daily-limit flag, bail if set to today's date
    	// update the user's reward info
    	// set daily-limit flag to today's date
    }
    

    @thinlizzie

    Participant

    Hi @markkavin,

    Thanks! Much appreciated!

    @robin-w

    Moderator

    no combo, so just hook separately 🙂

    @thinlizzie

    Participant

    @robin-w

    Cheers!

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