Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Members Loop Query

<?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?>

Question: How can this function be integrated into the members loop so that Female members are served a directory of males and male members are served a directory of females?**

    <?php
     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;

        // collection based on an xprofile field
        $custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND value = 'Female'");


        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 $count - $new_count;

    }
}

function custom_user_ids( ) {

    new BP_Custom_User_Ids ();

}

add_action( 'bp_before_directory_members', 'custom_user_ids' );

Example:

if {current user is "female" (filter members loop to show only males)}
          else if {current user is  "male" (filter loop to show only females)}
share|improve this question

closed as off-topic by birgire, toscho Jul 31 at 19:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
Your accepted answer was deleted. I have created another answer below that includes all the code. –  shanebp Dec 15 '13 at 15:37

3 Answers 3

up vote 2 down vote accepted
+250

Here is the solution that we discussed on http://buddypress.org/support/topic/filter-members-list-based-on-profile-field/

It is based on http://codex.buddypress.org/developer/bp_user_query/#code-examples

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
   $sex = xprofile_get_field_data( 5, bp_loggedin_user_id() );

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


    $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( ) {

    new BP_Custom_User_Ids ();

}

add_action( 'bp_before_directory_members', 'custom_user_ids' );
share|improve this answer
    
Nice solution shanebp –  henrywright Jan 6 at 3:36

You can use the 'include' parameter to pass a string of comma separated user IDs http://codex.buddypress.org/developer/loops-reference/the-members-loop/

You would have two loops on the page, conditionally showing the appropriate loop for each member depending on their gender.

Use the get_users() function to get your sets of user IDs:

http://codex.wordpress.org/Function_Reference/get_users

share|improve this answer
    
Ok henry. I'll do my best and update with my code. –  Aliyah Dec 8 '13 at 18:48
    
Yes, please do post once you're done coding if you need further help :) –  henrywright Dec 8 '13 at 19:38
    
updated question –  Aliyah Dec 11 '13 at 2:09
1  
I need further help. Thank you kindly sir –  Aliyah Dec 11 '13 at 7:23

// Replace include to user_ids

function custom_members_query( $query_array ) {
   // $query_array->query_vars['include'] = $this->custom_ids;
   $query_array->query_vars['user_ids'] = $this->custom_ids;
}


// Also replace this
// $custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND value = 'Female'");

// To
    $curr_user_val = $wpdb->get_var("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND user_id = ". get_current_user_id() );
    $custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND value != '$curr_user_val'");
share|improve this answer
    
Hi @Shazzad. I made the change and my directory is still showing all members. I need females to see only males in directory and males to see only females in directory so I need to add a function to the members loop, correct? I am female so I should not see myself in directory. –  Aliyah Dec 11 '13 at 17:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.