我有一个名为Departments的xprofile字段。department有三种类型的值(IT/ISM、Engineering、Others),因此,如果Engineering用户登录成员页,它将只显示正在进行工程的成员,当IT/ISM用户登录成员页时,它将只显示属于IT/ISM的成员,如果admin或其他人登录,它将显示成员页中的所有成员,目前工程正在运行,但如果IT/ISM用户登录,它会显示IT/ISM和其他,如果管理员登录,它会显示IT/ISM和其他,它不会向管理员显示工程,请指导我如何进行
请查看我的代码
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\' );
最合适的回答,由SO网友:shanebp 整理而成
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;
}
}
}