Skip to:
Content
Pages
Categories
Search
Top
Bottom

Total number of activity update posts by user


  • @mcuk
    Participant

    @mcuk

    Hi all,

    Attempting to display a TOTAL number of activity_update posts that the displayed user has ever posted and echo it in their profile header.

    This code by Brajesh at BuddDev has been used to do the same for obtaining the total number of favourites on the activities of the user (which can be used in conjunction with bp_displayed_user_id() ):

    function bpdev_count_user_acivity_favs( $user_id ){
        global $bp, $wpdb;
        $query =$wpdb->prepare("SELECT SUM(meta_value) as total FROM {$bp->activity->table_name_meta} WHERE meta_key = %s AND activity_id IN ( SELECT id FROM {$bp->activity->table_name} WHERE user_id = %d)", 'favorite_count', $user_id );
        return (int) $wpdb->get_var( $query );
    }

    Is it possible to alter the $query line to get the activity_update total count instead? Something like:

    $query =$wpdb->prepare("SELECT SUM(meta_value) as total FROM {$bp->activity->table_name_meta} WHERE meta_key = %s AND activity_id IN ( SELECT id FROM {$bp->activity->table_name} WHERE user_id = %d)", 'activity_update', $user_id );

    (I know the above code is wrong but you get the idea of what i mean).

    I’ve also looked at using <?php echo bp_activity_count( bp_displayed_user_id() ) ?> but its giving me a result of 0, so I’m not using it right :s .

    Any pointers appreciated!

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

  • danbp
    Participant

    @danbp

    You can use BuddyPress Member Stats plugin or just this function (in bp-custom.php)

    This example is for group updates only. But you have the general idea. And please, keep calm, if you have a big update writers community, totalizing ALL existing updates can became a mess and a real problem which can slow down a site. You’re warned ! 😉

    function bms_get_total_group_updates_count(){
    global $wpdb, $bp;
    $user_id = bp_displayed_user_id(); 
    
    	if ( bp_is_active( 'groups' ) ) :
    
    	// DB query 
    	$total_updates = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}bp_activity 
    			WHERE component = 'groups' 
    			AND   type = 'activity_update'
    			AND   user_id = '%s' ", $user_id ) );
    									  
    	echo $total_updates;
    
    	endif;
    }

    And for the output somewhere in your template, something like:

    <li><?php _e( 'My updates:', 'buddymemberstats' ); ?>&nbsp;<?php bms_get_total_group_updates_count(); ?></li>

    Yes, i’m the plugin author. 😉


    @mcuk
    Participant

    @mcuk

    haha great, thanks @danbp, works like a charm.

    I’ll definitely bear the speed issue in mind.

    Was considering using it as part of a gamification feature, ie. 100 posts, get a badge. 50 favourited posts, get a badge, etc.

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