要实现这一点,首先需要获取所有当前用户角色,X 和Y(s) 。。。
function get_current_user_roles(){
global $wp_roles;
$current_user = wp_get_current_user();
$user_groups = groups_get_user_groups( $current_user->ID );
$user_roles = isset( $current_user->roles[\'X\'] ) ? array( \'X\' ) : array();
// Let\'s get all Y dynamic user roles...
foreach ( $user_groups[\'groups\'] as $id ) {
$group = groups_get_group( array( \'group_id\' => $id ) );
$user_roles[] = \'bp_gm_\' . $group->slug . \'__\' . $group->id;
}
// Let\'s translate the roles...
if ( ! empty( $user_roles ) ) {
$translated_user_roles = array();
// I don\'t get why you have to check against `$wp_roles`...
foreach ( $user_roles as $role ) {
if ( isset( $wp_roles->role_names[ $role ] ) ) {
$translated_user_roles[] = translate_user_role( $role );
}
}
}
return $translated_user_roles;
}
现在我们可以
get_current_user_roles()
, 让我们实施我们的规范。。。
$user_roles = get_current_user_roles();
if ( count( $user_roles ) > 1 && isset( $user_roles[\'X\'] ) ) {
$args = apply_filters( \'moe_cars_args\', array(
\'post_type\' => \'cars\',
\'offset\' => ( max( 1, get_query_var(\'paged\') ) - 1 ) * $posts_per_page,
\'orderby\' => \'date\',
\'order\' => \'desc\',
\'author\' => implode( \',\', get_users( array(
\'role\' => $user_roles,
\'fields\' => \'ID\',
) ) ),
) );
}
希望这能回答你的问题。