Support » Plugin: BP Profile Search » Filtering Buddypress Widgets by Gender

  • Resolved outcold


    I’ve used the two code examples provided in the following post to successfully show members the opposite gender. However neither example works with the “(BuddyPress) Members” or “(BuddyPress) Recently Active Members” widgets. One uses ‘bp_before_directory_members’ and the other ‘bp_ajax_querystring’. I’m wondering if there’s a different hook I could employ or some other way to make this work with the stock BuddyPress widgets?



Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Andrea Tarantini


    Hello Marek,

    The BuddyPress widgets are outside the scope of BP Profile Search, anyway I had a look and this is what I found out.

    The members directory, the members widgets and other instances all use the members loop, as explained in:

    but the members directory is the only one using the bp_ajax_querystring() function and, as a consequence, the ‘bp_ajax_querystring’ filter.

    This allows BP Profile Search to filter the members directory, without affecting the other members loops.

    If we wish to filter the BP widgets instead, we’ll have to filter the standard members loop (using perhaps the ‘bp_pre_user_query_construct’ or the ‘bp_pre_user_query’ action), and that will likely affect all the other instances using the members loop. This could be desired or not, depending on our needs.

    So my suggestion is to experiment with the above mentioned action hooks, keeping an eye on the possible side effects.

    Hope this helps!

    Thank you SOOOOOOOOO much. I’d have never sorted that out on my own. Either the ‘bp_pre_user_query_construct’ or the ‘bp_pre_user_query’ action work for my purposes. I’m going to have to do a little testing and research to figure out which is the better choice but for now my widgets show Men for Women and visa versa. Here’s my current code running out of my theme’s functions.php I hope it’s of use to anyone else tackling this issue.

    class BP_Custom_User_Ids {
        private $custom_ids = array();
        public function __construct() {
            $this->custom_ids = $this->get_custom_ids();
            add_action( 'bp_pre_user_query_construct',  array( $this, 'custom_members_query' ), 1, 1 );
            add_filter( 'bp_get_total_member_count',    array( $this, 'custom_members_count' ), 1, 1 );
        private function get_custom_ids() {
            global $wpdb;
            // figure out if the logged-in user is male or female 
            // CHANGE FIELD id "3" BELOW
            $sex = xprofile_get_field_data( 3, bp_loggedin_user_id() );
            if ( $sex == 'Male' )
                // CHANGE FIELD id "3" BELOW
                $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 3 AND value = 'Male'";
                    // CHANGE FIELD id "3" BELOW
                    $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 3 AND value = 'Female'";
                    $custom_ids = $wpdb->get_col( $query );
                    return $custom_ids;
        function custom_members_query( $query_array ) {
            $query_array->query_vars['include'] = $this->custom_ids;
        function custom_members_count ( $count ) {
            $new_count = count( $this->custom_ids );
            return $new_count;
    function custom_user_ids( ) {
        // Don't do this for the admin
        if(!current_user_can('administrator') ) {
            new BP_Custom_User_Ids ();
    // add_action( 'bp_before_directory_members', 'custom_user_ids' );
    // add_action( 'bp_pre_user_query_construct', 'custom_user_ids' );
    add_action( 'bp_pre_user_query', 'custom_user_ids' );
    Plugin Author Andrea Tarantini


    Hi Marek,

    You are welcome! And thank you for sharing the solution you found.



    Hi Everyone.

    Thanks for sharing the code. Is anyone experiencing issues, with this code? I use Code snippet to insert codes and I have a code that works in on the member’s page. If anyone is interested!

    1. Bp search doesn’t work, with this code. It’s like there is no profile fields at all to search in and instead of showing – Nothing found. It shows all the members. One good thing though, is that it shows the opposite gender! The Bp search works for the admin, because he’s excluded, from the code.

    2. Buddypress widgets are now filtered by gender. However, the “members widget” have an issue. When you click on NEWEST, ACTIVE or POPULAR. It shows both genders. But when you update the page F5 or click on another link, it filters the widget back, so it shows the opposite gender again. Strange?

    So as Andrea said, there is a few side effects, at least on my site.
    Does anyone know how to fix this or have the same problem?

    Best regards Carsten. 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.