Question

I have a wordpress site set up, inside there is a master admin, then I have user Role Manager to change the Subscriber role to be able to add new users. I have then successfully restricted the subscriber to only making new users with a new role i have created "player".

I have then added a custom field to each user called "game". The subscriber can only change his and the "player"' custom field. the player cannot change this value.

I need to change the "All Users" page when a subscriber signs in, to only show the list of users that have the same cutom field value for "game".

I have tried all sorts of different plugins and tried my own dablle at changing code but nothing has worked :(

Any Help appreciated

Andy

Was it helpful?

Solution

Got the code from this Q&A, Remove Ability for Other Users to View Administrator in User List?.

You will need to tweak it a bit to adapt to your roles and capabilities, much probably changing the meta_key and meta_value values. I've added the check for $pagenow just to be sure this will only fire in /wp-admin/users.php.

add_action( 'pre_user_query', 'filter_users_wpse_10742' );

function filter_users_wpse_10742( $user_search ) 
{
    global $pagenow;
    if( 'users.php' != $pagenow)
        return;

    $user = wp_get_current_user();

    if ( $user->roles[0] != 'administrator' ) 
    { 
        global $wpdb;

        $user_search->query_where = 
        str_replace('WHERE 1=1', 
            "WHERE 1=1 AND {$wpdb->users}.ID IN (
                 SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta 
                    WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}user_level' 
                    AND {$wpdb->usermeta}.meta_value != 10)", 
            $user_search->query_where
        );
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top