Looping through ALL BP Users Profiles with a standalone functions

Hello!
I would like to loop through all my users and extract data they have entered for use in an array for another function. I understand that the members-loop.php does provide that 'structure' - but I really need to encapsulate that function and have it standalone. I feel that writing SQL would be over the top and prone to error (i.e. not good practice). I have created the following function in my child themes functions.php:

function getcustomfields()
{
	if ( bp_has_members() ) {
		while ( bp_members() ) : bp_the_member();
		bp_member_profile_data( 'field=Location 1' );
                echo 'Loop Test Loop ';
		endwhile;
	}
}

But when I call it, it does nothing (well its loops once - and I have 4 users created).
I'm guessing there is some kind of global I need to declare? Can anyone help?

  • jaseyb
    • WPMU DEV Initiate

    This is what I have done - although, I'm sure its probably a massive hack and against all good practices. Still, it may be useful to someone else in the interim.

    The following code section will select the id and display name of active users and then drag out an associated custom profile field (in this instance 'location 1').

    function getcustomfields()
    {
    	global $wpdb, $current_site, $post, $members_directory_base, $friends_enable_approval, $user_ID;
    	//$user_count = $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->base_prefix . "users WHERE user_status =0");
    	$user_count = $wpdb->get_results("SELECT id, display_name FROM " . $wpdb->base_prefix . "users WHERE user_status =0");
    
    	foreach ( $user_count as $user)
    	{
    		echo $user->display_name;
    		$location= xprofile_get_field_data( 'Location 1' ,$user->id);
    		echo $location.'</br>';
    	}
    
    }

    I hope someone else finds this useful - and I hope someone also tells me how to do this elegantly (with a code sample please!)

  • DavidM
    • DEV MAN’s Mascot

    Hi jaseyb,

    Thanks for posting your solution back here, I'm sure others will find that useful. It perfectly suitable to me with a cursory glance and I'm not aware of a function to get the user_status other than that myself.

    I'll have a closer look at the code myself when I get a free moment, but glad that works for you thus far.

    -David

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.