@emmanuel1234 you will need to customize directory based on logged in user xprofile values.
you can check https://codex.buddypress.org/developer/loops-reference/the-members-loop/
@emmanuel1234, I did the exact same thing on my site but I took it a step further. For example, if the user is a man who is looking for a woman, then the directory will only show women who are looking for men, not just all women (some women might be looking for other women etc.).
Step 1: The below code is in my bp_custom.php where field #193 is the user’s gender and field #197 is the gender the user is looking for. Replace them with your field IDs.
function get_desired_users () {
global $wpdb;
$my_user_id = bp_loggedin_user_id();
$my_gender = $wpdb->get_var("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 193 AND user_id = $my_user_id");
$i_am_looking_for = $wpdb->get_var("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 197 AND user_id = $my_user_id");
//Get all users that are what the logged_in member is looking for
$my_matches = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 193 AND value = '$i_am_looking_for' AND user_id IN (SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 197 AND value = '$my_gender')");
$include = implode(",", $my_matches);
return $include;
}
Then in your members_loop page, in the bp_ajax_querystring line add the function that returns the user IDs like so:
<?php if ( bp_has_members( bp_ajax_querystring( 'members' ) .'&include=' .get_desired_users() ) ) : ?>
That works like a charm for me.
hi thank you is there a way i can set the members directory to a show a age range of five years as default for example a user register and select their age at (20) now when the users go to the members directory page can the member directory show only a age range of 20 to 25 of users who’s ages are 20,21 ,22,23,24,25 as the default
Give this a try. I didn’t validate it but it should work. Let me know.
Field #2 is the birthday field.
function get_desired_users () {
global $wpdb;
$my_user_id = bp_loggedin_user_id();
$my_gender = $wpdb->get_var("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 193 AND user_id = $my_user_id");
$i_am_looking_for = $wpdb->get_var("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 197 AND user_id = $my_user_id");
$my_birthday = $wpdb->get_var("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND user_id = $my_user_id");
$five_years_older = date("Y-m-d H:i:s", strtotime("-5 years", strtotime($my_birthday)));
//Get all users that are what the logged_in member is looking for
$my_matches = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 193 AND value = '$i_am_looking_for' AND user_id IN (SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 197 AND value = '$my_gender') AND user_id IN (SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value BETWEEN '$five_years_older' AND '$my_birthday')");
$include = implode(",", $my_matches);
return $include;
}
I posted this and I realized I’m pulling a birthday field (1974-03-10 00:00:00), not an age number like 20, but it’s the same principal. If your field is an age number field then just replace $five_years_older = date("Y-m-d H:i:s", strtotime("-5 years", strtotime($my_birthday)));
with $five_years_older = $my_birthday + 5;
and the use this query instead:
$my_matches = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 193 AND value = '$i_am_looking_for' AND user_id IN (SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 197 AND value = '$my_gender') AND user_id IN (SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value BETWEEN '$my_birthday' AND '$five_years_older')");
For the sake of clarity, in this case $my_birthday should be replaced with $my_age. I know, OCD 🙂