Tulip Media

Entrepreneurial thoughts

Snippet BuddyPress: How to only show people of the opposite gender in the members page

Looking to build a dating site with BuddyPress and WordPress? Unless it's a site where the concept is based on being open to all genders, you will necessarily need to customize BuddyPress so that men can only see women, and vice versa.

BuddyPress does not allow people to be filtered by gender in the search by default, and therefore cannot prevent people of the same gender from seeing each other each other in the members page.

Furthermore, there is currently no WordPress plugin that allows this. Fortunately, the person behind the excellent BP Profile Search plugin, which allows users of your social network to search for members, has shared a very useful piece of code. The latter will not display only people of the opposite sex in the members page, and will prevent people of the same sex from seeing each other.

To use it, you will need to create a file called “bp-custom.php”, which you will upload to the /wp-content/plugins/ folder of your WordPress installation.

In this bp-custom.php file, here is the code that must be put between the 2 php tags (don't forget them!):

[coded]

add_filter('bp_ajax_querystring', 'modify_members_loop', 20, 2);
function modify_members_loop ($qs=false, $object=false) {

if (is_user_logged_in() && !current_user_can('activate_plugins') ){

global $wpdb;
if ($object != 'members') return $qs;

// figure out if the logged-in user is male or female
$gender = xprofile_get_field_data(2, bp_loggedin_user_id());

if ($gender == 'Male')
$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value = 'Female'";
else
$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value = 'Male'";

$custom_ids = $wpdb->get_col ($query);

$args = wp_parse_args($qs);
$args['include'] = implode(',', $custom_ids);
$qs = build_query($args);

return $qs;
}
}

[/coded]

Be careful, in this code, you will need to replace all “2”s with the ID corresponding to the “genre” field of your BuddyPress configuration. To find the ID, go to the list of “profile fields”, and look at the URL of the “Gender” field, if it is the “Gender” field that you want to lock.

Replace “Male” and “Female” with the exact values you are using in the “Genre” field.

Finally, you will be able to observe the result by logging in as a member, because in this code, we have deliberately excluded the admin in order to that as an admin, you can see all the members who make up your social network or your dating site! Practical, isn't it?

Once this code is uploaded, you will see that only members of the opposite sex will display in the members loop. However, this code does not filter other loops displaying members (widgets displaying the last members, etc.).

If you enjoyed my tutorial, don't hesitate to let me know on Twitter And Facebook !

 

My latest tweets

Nouveau short sur Blooness 👉 Relancer la libido, en vrac -> Soleil, vitamine D, diète céto-méditerranéenne, protéines, livres, badinage, marche, HIIT, et pas de régime hypocalorique.
https://buff.ly/445PWvc

Nouveau short sur Blooness 👉 Améliorer son sommeil -> s’exposer à la lumière le matin, limiter les lunettes de soleil, mouiller la chemise au sport, boire du déca, manger maximum 3h avant de dormir, faire une marche digestive et prendre de la mélaton
https://buff.ly/440weAJ

Load more

Don't miss business advice by email

No spam, just an email when new content is published.

2 Responses

  1. Good morning,

    This is a very practical function.

    On the other hand, this creates a conflict with the “buddypress followers” plugin.

    This plugin allows members to add themselves as favorites and when you use your code, it displays all members as favorites.

    Do you think this can be fixed?

    I thank you in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEN