BuddyDev

Search

How get I get the total activity count of a user

  • Blocked
    Level: Initiated
    Posts: 3
    Olamide on #33529

    I want to get the total activity count for users how can I do this

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #33536

    HI,
    Thank you for the question.

    There does not exist a function in BuddyPress API to achieve this currently.

    We can create own own like this

    
    /**
     * Returns total activities count for the user
     *
     * @param int $user_id user id.
     * @param string $type activity type.
     *
     * @return int
     */
    function buddydev_get_total_user_activities_count( $user_id, $type = '' ) {
    	global $wpdb;
    
    	$table = buddypress()->activity->table_name;
    
    	$where_conditions = array();
    
    	$where_conditions[] = $wpdb->prepare( 'user_id = %d', $user_id );
    	if ( $type ) {
    		$where_conditions[] = $wpdb->prepare( "type = %s", $type );
    	}
    
    	$whe_sql = join( ' AND ', $where_conditions );
    
    	return absint( $wpdb->get_var( "SELECT count(DISTINCT id) FROM {$table} WHERE {$whe_sql}" ) );
    }
    
    

    and use it like

    
    
    $updates_count = buddydev_get_total_user_activities_count( 1, 'activity_update' );
    $all_count = buddydev_get_total_user_activities_count(1)
    

    and so on.

    Regards
    Brajesh

  • Blocked
    Level: Initiated
    Posts: 3
    Olamide on #33549

    Count not showing

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #33551

    You need to print that using echo.

    Example:-

    
    echo "Count:" . buddydev_get_total_user_activities_count( 1, 'activity_update' );
    

    Where 1 is user id.

  • Blocked
    Level: Initiated
    Posts: 3
    Olamide on #33565

    It’s working now thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #33576

    Thank you for confirming.

    Regards
    Brajesh

The topic ‘How get I get the total activity count of a user’ is closed to new replies.

This topic is: not resolved