Skip Navigation

[Résolu] I want to add a dropdown with a list of all members with a certain role.

This support ticket is created Il y a 7 années. 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.

This topic contains 9 réponses, has 4 voix.

Last updated by christopherB-5 Il y a 6 années et 11 mois.

Assigned support staff: Waqas.

Auteur
Publications
#322411

I am trying to: make a dropdown that's populated with all users belonging to a certain group. Is this possible, and how?

#322442

Thanks for contacting support forum.

It is not possible within CRED or Views yet, It is already in our to-do list, I add this thread into it. Currently, you need try create custom PHP code to apply this functionality.

For more information, please refer: https://codex.wordpress.org/Function_Reference/get_users

Regards
Ankit G.

#322706

It would be great if i could somehow do this:

Users with the role "members"
Users with the role "coach"

I want the admins of the site to be able to select a coach in a member's profile.
Doesn't need to be direct; maybe a user that get's the "coach" role can automatically create a taxonomy item?

I'm not sure what to do best.

#322879

This will require custom scripting which falls beyond the support we provide at toolset support forum.

At this point I would suggest you consider contacting one of our certified partners from this link: https://toolset.com/consultant/

#324014

it required a dropdown added to the user with the id "thisidrighthere"

and this code in functions.php.

add_filter('option_wpcf-usermeta', 'fill_my_users');
function fill_my_users($fields) {
foreach ($fields as &$field) {
if ($field['id'] == 'thisidrighthere') {
$field['data']['options'] = array();
$users = get_users();
foreach ($users as $user) {
$field['data']['options'][$field['id'] . $user->user_login] = array('title' => $user->user_login, 'value' => $user->ID);
}
}
}
return $fields;
}

I'm just not sure yet how to get only the users within a certain role.

#325434

Waqas
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

You can use 'role' parameter of get_users() to fetch only the users having a certain role, like below:

$users = get_users('role= members');

Please see https://codex.wordpress.org/Function_Reference/get_users for more information.

#328685
Schermafbeelding 2015-08-26 om 17.35.20.png

I have:

add_filter('option_wpcf-usermeta', 'fill_my_users');
function fill_my_users($fields) {
foreach ($fields as &$field) {
if ($field['id'] == 'fillthecoachin5') {
$query_args = array();
$query_args['fields'] = array( 'ID', 'display_name' );
$query_args['role'] = 'Coach';
$users = get_users( $query_args ); {
foreach ($users as $user) $users_array[$user->ID] = $user->display_name;
}
}
}
return $fields;
}

in functions.php and (screenshot) in types, still no population of the dropdown ( i get the dropdown just nout filled with users)

Am i missing something?

#328866

Waqas
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

Please note down the correct and working code:

add_filter('option_wpcf-usermeta', 'fill_my_users');
function fill_my_users($fields) {
	$options = array();

	foreach ($fields as &$field) {
		if ($field['id'] == 'fillthecoachin4') {
			$query_args = array();
			$query_args['fields'] = array( 'ID', 'display_name' );
			$query_args['role'] = 'Coach';
			$users = get_users( $query_args );

			$i = 1;

			foreach ($users as $user) {
				$optionID = "wpcf-fields-select-option-".time()."-".$i;
				$options[$optionID] = array(
					"title" => $user->display_name,
					"value" => $user->ID
				);

				$i++;
			}

			$field['data']['options'] = $options;
		}
	}

	return $fields;
}

Please notice that field name is 'fillthecoachin4' in the screen shot while you used 'fillthecoachin5' - I have corrected in above code. Also there are new additions in the code, which are required to generate appropriate options for the field.

#329174

AWESOME
Thank you very much, it works perfectly.

#437031

Has this feature been implemented yet? I am looking to do the same thing.

Thanks