Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Get activity_comment count for user


  • graemebryson5
    Participant

    @graemebryson5

    Hi,

    I’m attempting to pull in a user’s activity_comment count into a bar on their profile alongside a post count and favourite count using the following snippet:

    //* Get Comment count for profile stats
    function get_user_comment_count( $user_id ) {
      $args = array(
        'per_page' => 10000,
        'show_hidden' => true,
        'user_id' => $user_id,
        'action' => 'activity_comment'
      );
    
    if ( bp_has_activities( $args ) ) {
      global $activities_template;
      $count = $activities_template->total_activity_count;
    } else {
      $count = 0;
    }
    return $count;
    
    }

    I’m implementing it with:

    <?php echo get_user_comment_count( bp_displayed_user_id() ) ?>

    On testing, this seems to cause an error as the page fails to load beyond the point of implementation. Could anyone help point me in the right direction to get it up and running?

Viewing 3 replies - 1 through 3 (of 3 total)

  • danbp
    Participant

    @danbp

    @graemebryson5,

    you need a function to fetch the activity_comment type in bp_activity table. For the count itself, we use mysql count. Here it goes:

    function bpfr_get_activities_comment_count() {
         global $wpdb;
         $user_id = bp_displayed_user_id(); 
    
    	if ( bp_is_active( 'activity' ) ) {
    
    	   // DB query 
    	   $total = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$wpdb->prefix}bp_activity WHERE user_id = '%d' AND type = 'activity_comment'", $user_id ) );
    									  
    	   return $total;
    	
    	}
    }

    Now you can echo bpfr_get_activities_comment_count wherever you need it on your profile template.

    Here an example to get it on profile header.

    function bpfr_total_comments() {
       echo 'My comments: '. bpfr_get_activities_comment_count();
    }
    add_filter( 'bp_before_member_header_meta', 'bpfr_total_comments' ); 

    If you need more member activities counts, try Buddy Member Stats !


    graemebryson5
    Participant

    @graemebryson5

    Absolutely perfect. Thanks for this Dan, I’ve been struggling with it for a few days now so it’s massively appreciated.


    danbp
    Participant

    @danbp

    De nada. You’re welcome ! 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Get activity_comment count for user’ is closed to new replies.
Skip to toolbar