Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display total number of contributors


  • dta_madrid
    Participant

    @dta_madrid

    I’m looking for a way to display the total number of contributors in a sidebar.

    I guess I need to start off with using this:

    bp_total_member_count, but to exclude certain the other user types?

    Thanks for your help,
    (p.s. I’m using Budddypress 1.8.1)

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

  • shanebp
    Moderator

    @shanebp

    You’ll need some custom sql. Put this in bp-custom.php

    function contributor_count() { 
      global $wpdb;
    		
      $contributor_count = $wpdb->get_var( "SELECT COUNT(user_id) FROM {$wpdb->prefix}usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' AND meta_value LIKE '%contributor%'" );
    	
      return $contributor_count; 
    }

    Use it wherever you want like this:

    echo contributor_count();


    dta_madrid
    Participant

    @dta_madrid

    Sorry, I thought i replied to this!; much appreciated, thanks.


    Henry Wright
    Moderator

    @henrywright

    @dta_madrid

    Further to @shanebp’s method, WordPress has a function called count_users() which can be used to get the number of users in each role.

    https://codex.wordpress.org/Function_Reference/count_users

    $users = count_users();
    
    // display total users (if you want to).
    echo $users['total_users'] . ' total users';
    
    // display count for contributors. 
    foreach( $users['avail_roles'] as $role => $count ) {
        if ( $role == 'contributor' )
            echo $count . ' contributors';
    }

    shanebp
    Moderator

    @shanebp

    @henrywright
    Much better approach – thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display total number of contributors’ is closed to new replies.
Skip to toolbar