govindak
govindak

Reputation: 452

how to filter users created by admin in wordpress

Is there way to separate the users created by admin in wordpress, eg admin1 created 3 editors and admin2 creates 5 editors the admin1 shouldnot see the users created by admin2 or vice versa.

Upvotes: 1

Views: 561

Answers (2)

Anunay
Anunay

Reputation: 507

Please add the following code in your functions.php

        add_action( 'user_register', 'ad_registration_save', 10, 1 );
        function ad_registration_save( $user_id ) { 
                $current_admin_user = wp_get_current_user();
            if ( isset( $_POST['first_name'] ) ){
                $user = new WP_User( $user_id );
                $user_roles = $user->roles;
                $user_role = array_shift($user_roles);
                if($user_role!="administrator"){
                    update_user_meta($user_id, 'asso_admin', $current_admin_user->ID);  
                }else{
                    update_user_meta($user_id, 'asso_admin', $user_id); 
                }        
            }
        }


        add_action('pre_user_query','adminuser_pre_user_query');
        function adminuser_pre_user_query($user_search) {
            if(is_admin()){
              $user = wp_get_current_user();
              if($user->ID!=1){
                global $wpdb;
                $user_search->query_from .= "JOIN {$wpdb->usermeta} um ON um.user_id = {$wpdb->users}.ID AND um.meta_key = 'asso_admin'";
                $user_search->query_where = 'WHERE um.meta_value=' . $user->ID;         
              }
            }
        }

Upvotes: 3

brasofilo
brasofilo

Reputation: 26065

You'll have to:

Upvotes: 0

Related Questions