Support

Account

Home Forums General Issues User field – Filter and show only same role users

Solving

User field – Filter and show only same role users

  • Hi!
    I have a front-end form with a user field for team leaders to select which of their team members (“asistentes”) have attended a meeting. Each team has a different user role assigned (“Albacete”, “Berja”).

    What I need is that the field only shows the members that have the same role as the team leader that is filling in the form.

    Any idea how could I achieve this?
    Thanks in advance!

  • There is an undocumented filter for the user field, most of the user field is not documented.

    Anyway, there is a filter that you can use to alter the results for the user field, here is where it’s called in the user field code

    
    $args = apply_filters("acf/fields/user/query",							$args, $field, $options['post_id']);
    		$args = apply_filters("acf/fields/user/query/name={$field['_name']}",	$args, $field, $options['post_id']);
    		$args = apply_filters("acf/fields/user/query/key={$field['key']}",		$args, $field, $options['post_id']);
    

    $args is the same as the arguments used in the WP function get_users(), https://developer.wordpress.org/reference/functions/get_users/

    In your filter you would get the current user roles and then alter the arguments to include only other users with the same role, I think with the “role__in” argument, but I’m not sure.

    Sorry I cannot be more specific, but I only know that the filter exists, I’ve never used it.

  • Hi John,
    thanks for your help.
    I’ll try to solve it using your guidelines and I’ll post the solution here if I get to it.

    Regards

  • Hey,
    Has there been any progress made on this filtering? I have a very similar request.

    Thanks

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.