• Hello, I’ve got a little coding question.

    I created a function on add form action to fill a list of users but I would like to filter only users with subscriber role. I could I add this filter ? Here’s my initial code:

    private function set_users() {
            $this->load_model('User');
            $users = $this->User->find(array('selects' => array('ID','display_name')));
            $this->set('users', $users);
        }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You don’t need an actual WP filter if that is what you mean by “filter”. You can get an array of users fitting any number of constraints (including role) with the WP_User_Query class. Then loop through the resulting array to fill your list.

    Thread Starter yoyos33

    (@yoyos33)

    I don’t talk about WP filters but just conditions with the find method of User model

    I think we should avoid using WP class if we’re using the WP-MVC plugin, right ?

    My aim is to fill a select option list of users with role subscriber.

    Moderator bcworkz

    (@bcworkz)

    I’m not familiar with MVC, so I don’t know about any restrictions on using WP classes. On the surface this strikes me as ridiculously constricting, nearly everything we do in WP involves a class somehow. WP_User_Query will do what you want, disregarding any MVC restrictions. You could also use a direct SQL query to get the users you want, which is all the WP_User_Query class really does. But if you use the global $wpdb object to make the query, you are still using the WP class wpdb. Of course you could connect to the DB using just PHP if you really need to avoid all WP classes.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Find users by role’ is closed to new replies.