BuddyDev

Search

[Resolved] Loop for member types

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #556

    Hi Brajesh,

    First of all thanks for the member type generator plugin, it helped me creating the member types i needed.

    I am a php/buddypress n00b and I am stuck with something.

    The case is as follows:

    1) I have created 3 member_types (teacher / student / parent )
    2) I have an array (csv) of users

    what i am looking to accomplish is to loop the ID’s and get as result the count and array of user ID’s for each membertype

    so e.g.
    4 teachers, ID’s 1,2,3,4
    5 students, ID’s 5,6,7,8,9
    2 parents, ID’s 10,11

    Also i was wondering if it is possible to set a default member type, so after registration users will always be ‘ student’ unless the admin changes it to teacher or parent

  • Keymaster
    (BuddyDev Team)
    Posts: 24599
    Brajesh Singh on #563

    Hi,
    Welcome to BuddyDev support forum.

    1. Do you need ids or just the count? If you need just the count here is a function with example to do that

    
    
    function buddydev_get_member_types_count( ) {
    	
    	
    	$member_types = get_terms( 'bp_member_type', array(
    		'fields'		=> 'all',
    		'hide_empty'	=> false
    	));
    	
    	if( ! $member_types ) {
    		return array();
    	}
    	
    	$counts = array();
    	foreach ( $member_types as $member_type ) {
    		$counts[ $member_type->slug] = $member_type->count;
    	}
    
    	
    	return $counts;
    }
    
    

    You can put the above code in your bp-custom.php or theme’s functions.php.

    And use the following code to show the counts

    
    
    //example usage in your file
    $counts = buddydev_get_member_types_count();
    
    echo "Total students:" . $counts['student'];
    
    

    Please do note, if you are on multisite, this code will only work on main site.

    About settings the default member type, yes.

    
    
    add_action( 'bp_core_activated_user', 'buddydev_set_default_member_type', 1 );//high priority
    
    function buddydev_set_default_member_type( $user_id ) {
    	
    	bp_set_member_type( $user_id, 'student' );
    }
    
    

    Please put that code in your bp-custom.php and that should take care of the default assignment.

    Hope that helps.

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #571

    Hi Brajesh,

    Thanks, i will give it a try. It will surely be a help!

    As per my openings post, I am also after getting the user ID’s as i want to use them in a memberloop in a nav tab.

    Thanks so far for the great help! It is really appreciated!

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #572

    Hi Brajesh,

    I have tested the (counting) code and it seems to be working. However, it returns a total count of all members.

    What was after is to get the results for a specific array of user ID’s, and breakdown this array:

    E.g.

    $res2 = array(1,2,3,4,5,6,7,8,9,10,11)

    4 teachers, ID’s 1,2,3,4
    5 students, ID’s 5,6,7,8,9
    2 parents, ID’s 10,11

    so i need a way to tell your function to only check the ID’s as in $res2

  • Keymaster
    (BuddyDev Team)
    Posts: 24599
    Brajesh Singh on #577

    Hi eGuard,
    Here is an example function

    
    
    function buddydev_custom_get_users_by_member_types( $ids = array() ) {
    		
    	$ids = wp_parse_id_list( $ids );
    	
    	if( ! $ids ) {
    		return ;
    	}
    	
    	$args = array(
    		'user_ids'			=> $ids,
    		'populate_extras'	=> true, //it will fetch names etc and cache member types
    	);
    	
    	$user_query = new BP_User_Query( $args );
    	
    	$users = $user_query->results;
    	$user_by_types = array();//multidimensional assay
    	//an Arr of WP_User objects with some added details like last_activity, latest_update etc
    	foreach ( $users as $user ) {
    		
    		$member_type = bp_get_member_type( $user->ID );
    		if( ! $member_type ) {
    			continue;
    		}
    		
    		$user_by_types[$member_type][] = $user->ID;
    		
    	}
    	
    	print_r( $user_by_types );
    	
    }
    
    

    And you ca call it like this

    
    <?php buddydev_custom_get_users_by_member_types( '1,2,3,4,5,6,7,8');?>
    

    Hope that helps you.

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #609

    Hi Brajesh,

    Thanks for your guidance.

    Based on your code I managed to do what i looked to accomplish.
    Thanks for the great support!

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #611

    Sorry,

    one last question in regards to this subject.

    Based on your example, i have created a function where i can specify the member type

     
    function get_user_id_by_member_types( $ids = array(), $type ){
    
    	
    	$ids = wp_parse_id_list( $ids );
    	
    	if( ! $ids ) {
    		return ;
    	}
    	
    	$args = array(
    		'user_ids'			=> $ids,
    		'populate_extras'	=> true, //it will fetch names etc and cache member types
    	);
    	
    	$user_query = new BP_User_Query( $args );
    	
    	$users = $user_query->results;
    	$user_by_types = array();//multidimensional array
    	//an Arr of WP_User objects with some added details like last_activity, latest_update etc
    	foreach ( $users as $user ) {
    		
    		$member_type = bp_get_member_type( $user->ID );
    		if( ! $member_type ) {
    			continue;
    		}
    		
    		$user_by_types[$member_type][] = $user->ID;
    		
    	}
    	
    	// print_r( $user_by_types );
    
    	$type_result = $user_by_types[$type];
    	//print_r ($type_result); 
    $csv = implode( ',' , $type_result );
    
    echo $csv;
    }
    

    It echo’s a comma seperated list of ID’s for the type.

    Your member type generator plugin can also have a member directory for each member type.
    What i would like to do is to have a member directory (just like yours) in a navigation tab in the displayed user profile.

    So i have created a nav tab called “students”, and as content of the nav tab i would like to have the directory of all users in the array $csv of the type students as defined in $type.

    I know where to put the content code for this tab, but i don’t know how to query the memberloop for both the $type (in this case students) and the ID’s in $csv.

    Could you give me a pointer in the right direction?

You must be logged in to reply to this topic.

This topic is: resolved