admin管理员组

文章数量:1410682

I have a xprofile field called Departments the department has three types of value (IT/ISM,Engineering,Others),so if engineering user logged in member page it will show only the members who are Engineering ,when IT/ISM user logged in member page it will show only the members who are IT/ISM ,if admin or others logged in it will show all the members in the member page ,right now engineering is working but if IT/ISM user logged in it shows IT/ISM and Others ,if admin logged in it shows IT/ISM and Others it not showing engineering to admin and please guide me how to make it

Please see my code

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 engineer or it
   $s = xprofile_get_field_data( 2, bp_loggedin_user_id() );

   if ( $s == 'IT/ISM' ) 
      $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value = 'IT/ISM'";


   else
       $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value = 'Engineering'";    



    $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' );

I have a xprofile field called Departments the department has three types of value (IT/ISM,Engineering,Others),so if engineering user logged in member page it will show only the members who are Engineering ,when IT/ISM user logged in member page it will show only the members who are IT/ISM ,if admin or others logged in it will show all the members in the member page ,right now engineering is working but if IT/ISM user logged in it shows IT/ISM and Others ,if admin logged in it shows IT/ISM and Others it not showing engineering to admin and please guide me how to make it

Please see my code

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 engineer or it
   $s = xprofile_get_field_data( 2, bp_loggedin_user_id() );

   if ( $s == 'IT/ISM' ) 
      $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value = 'IT/ISM'";


   else
       $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value = 'Engineering'";    



    $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 question edited Jun 16, 2015 at 11:04 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jun 16, 2015 at 9:01 RajaRaja 1036 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try:

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 engineer or it
       $s = xprofile_get_field_data( 2, bp_loggedin_user_id() );

        if( is_super_admin() ) {
              $custom_ids = false;
        }
        elseif ( $s == 'IT/ISM' ) {
              $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value = 'IT/ISM'";
              $custom_ids = $wpdb->get_col( $query );
        }
        elseif ( $s == 'Engineering' ) {
              $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 2 AND value = 'Engineering'";    
              $custom_ids = $wpdb->get_col( $query );
        }
        else {
           $custom_ids = false;
        }

        return $custom_ids;
    }

    function custom_members_query( $query_array ) {

        $query_array->query_vars['include'] = $this->custom_ids;

    }  

    function custom_members_count ( $count ) {

        if( $this->custom_ids == false )
            return $count;
        else {
            $new_count = count( $this->custom_ids );
            return $new_count;
        }
    }
}

本文标签: pluginsBuddyPress filter member based on Xprofile Field