SOLVED: Limit Members Loop by Profile Fields
-
After long hours trying to fix this issue, I present to all who care, a simple way to only include only members with certain profile field values in a loop. I tried assigning roles to certain users through custom profile fields, and then pull only those certain members on specific listing pages.
What I tried first was including an if statement within the loop and only showing the members info of those assigned the specific role. It worked – to a point. The problem came in with pagination where the loop was still recognizing all members that were queried, and creating pages to list them even though those pages were empty since these members did not meet the if condition and therefore were not displayed.
So, I needed a different approach which I present to you here. Place this in your functions.php and you’re good to go. It runs a quick query on all members and returns a comma separated list of member id’s that fit the criteria to give you a list of id’s to include. See example below.
`
/* Include only members that have a specific profile value in a specific profile field in a loop.
* $theMetaValue = Value of field to search for
* $theMetaFielt = Field name to search for Meta Value
* EXAMPLE OF CODE IN ACTION:
* include_by_meta(‘Profile Field Value’, ‘Profile Field Name’), ‘type’ => ‘newest’) )) : ?>
*
*
*
*
* Credit: http://www.duable.com
*/function include_by_meta($theMetaValue, $theMetaField) {
$memberArray = array();
if (bp_has_members()) :
while (bp_members()) :
bp_the_member();
$theFieldValue = bp_get_member_profile_data( ‘field=’. $theMetaField );
if ($theFieldValue==$theMetaValue) {
array_push($memberArray, bp_get_member_user_id());
}
endwhile;
endif;$theIncludeString=implode(“,”,$memberArray);
return $theIncludeString;
}
?>
`
You must be logged in to reply to this topic.