Is there a way to remove/hide the list of admins without SQL queries with ‘pre_user_query’ if a user has list_user caps but not admin caps?

Question

There are examples on WordPress Stackexchange that suggest code such as:

/** Hide Administrator From User List **/
function isa_pre_user_query( $user_search ) {
    if ( !current_user_can( 'administrator' ) ) { // Is Not Administrator - Remove 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}capabilities'
              AND {$wpdb->usermeta}.meta_value NOT LIKE '%administrator%' )", 
            $user_search->query_where
        );
    }
}

add_action( 'pre_user_query', 'isa_pre_user_query' );

There’s also a tutorial from Misha Rudrastyh with a similar approach.

Is there a way to achieve a similar outcome with a native WordPress function?

0
Motivated 3 weeks 2022-08-31T15:54:35-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse