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

New shorts on Blooness 👉 Relaunch libido, in bulk -> Sun, vitamin D, keto-Mediterranean diet, proteins, books, banter, walking, HIIT, and no hypocalorie diet.
https://buff.ly/445PWvc

New shorts on Blooness 👉 Improve your sleep -> expose yourself to light in the morning, limit sunglasses, wet your shirt during sports, drink decaf, eat maximum 3 hours before sleeping, take a digestive walk and take some melaton
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