Viewing 3 reply threads
  • Author
    Posts
    • #3867
      vgrch
      Member

      Hello,

      I need to limit list of users displayed in dropdown by role. I added
      'role' => 'my_custom_role'
      to get_users array, but it didn’t work.

      How can I make it work?

      Thanks.

    • #3870
      Steve
      Keymaster

      @vgrch– The role parameter sets which user role can see the field. So if you set it to “administrator”, and the logged in user is an “editor”, the field won’t display.

      I’m assuming you are basing your field off this tutorial >

      If so, then the WordPress function get_users() is what is actually generating the user list. get_users() has a role parameter that you can use. Based on that tutorial, you would modify your code to look like this:

      piklist('field', array(
       'type' => 'select'
       ,'field' => 'my_users'
       ,'label' => 'Choose a user'
       ,'attributes' => array(
         'class' => 'text'
       )
       ,'choices' => array(
          '' => 'Choose User'
        )
        + piklist(
         get_users(
           array(
            'orderby' => 'display_name'
            ,'order' => 'asc'
            ,'role' => 'author' // Only show authors in this select field.
           )
           ,'objects'
         )
         ,array(
           'ID'
           ,'display_name'
         )
        )
      ));
      

      Let us know if you need any more help.

    • #3877
      vgrch
      Member

      I found out it was my fault as I forgot to assign the role to the users.

      Thanks for the reply.

    • #3878
      Steve
      Keymaster

      Glad it all worked out. Closing ticket.

Viewing 3 reply threads
  • The topic ‘Limit list of users by role’ is closed to new replies.