Skip Navigation

[Resolved] Duplicate custom function for various roles.

This thread is resolved. Here is a description of the problem and solution.

Problem:

Get users from multiple roles using PHP codes.

Solution:

It is a custom PHP codes problem, see solution here:

https://toolset.com/forums/topic/duplicate-custom-function-for-various-roles/#post-1188720

Relevant Documentation:

https://developer.wordpress.org/reference/functions/get_users/

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Edward Barker 5 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1188231

Tell us what you are trying to do?

Basically I'm trying to duplicate a custom snippet for different parameters.

I have a custom post type called "Review" and within that post I want to create custom select fields to assign users to different job roles of that review based on their wordpress roles (defined my ultimate member).

There are 3 roles I want to add ......

Chair. UM Role: um_chair Custom Field Slug: list-of-users-chair
Expert by Experience. UM Role: um_expert-by-experience. Custom Field Slug: list-of-users-ebe
Independent Clinician. UM Role: um_independent-clinician. Custom Field Slug: list-of-users-ic

I've used the following threads for reference.

https://toolset.com/forums/topic/types-create-a-custom-field-and-have-a-dropdown-of-users-as-value/ (used for bulk of the code)
https://toolset.com/forums/topic/filter-by-author-for-only-custom-user-type/ (Used to add role filter)

Using the threads I created the first drop down select list with no issues using the following complete code ........

add_filter('option_wpcf-fields', 'fill_my_users');
function fill_my_users($fields) {
foreach ($fields as &$field) {
if ($field['id'] == 'list-of-users-chair') {
$field['data']['options'] = array();
$users = get_users( 'role=um_chair' );

foreach ($users as $user) {
$field['data']['options'][$field['id'] . $user->user_login] = array('title' => $user->user_firstname . ' ' . $user->user_lastname, 'value' => $user->ID);
}
}
}
return $fields;
}

My only issue is that I don't know how to replicate it for the other two roles (Expert by Experience and Independent Clinician). Can you help at all, can it all be done with one snippet or does it have to be separate snippets for the two other roles?

Cheers
Ed

#1188720

Hello,

In your custom PHP codes, you are using function get_users(), see wordpress document:
https://developer.wordpress.org/reference/functions/get_users/#comment-content-2112

In the bottom of above page, there is an example codes to get multiple user roles:

...
get_users( [ 'role__in' => [ 'author', 'subscriber' ] ] );
...

For your reference.

#1188948

Brilliant. Thank you!