First you can save in a associative array the value of the school field and the user id who belongs this field value. You need to know the school field ID.
define(FIELD_ID, /* Insert field id here */);
/* Save all site users */
$users = bp_core_get_users();
/* Create array for save fields values */
$user_xprofile_school = array();
/* Loop over each user and save the id and the school field value */
foreach ( $users as $user ):
$index = 0;
foreach ( $user as $u ):
/* Check if value exists before save */
if ( xprofile_get_field_data( FIELD_ID, $u->id, 'array' ) ):
$users_xprofile_school[$index]['user_id'] = $u->id;
$users_xprofile_school[$index]['field_school'] = xprofile_get_field_data(FIELD_ID, $u->id, 'array');
$index++;
endif;
endforeach;
endforeach;
This is going to return an array like this:
array([0] => ([user_id] => 1, [field_school] => 'years 2017'), [1] => ([user_id] => 2, [field_school] => 'years 2018'))
Then you can loop the users posts using the field_school
index and the user_id
.
This is not all the work but could be a good start point. Hope this help you.