This plugin provides a Field Type where you can select one or more user groups. To create the field, go to Settings → Fields in your control panel.
There is 3 display modes:
To list the group(s) selected:
{% set groups = entry.userGroupFieldHandle.getGroups() %}
{% for group in groups %}
{{ group.name }}
{% endfor %}
To get only the first group:
{% set groups = entry.userGroupFieldHandle.getGroups() | first %}
{{ group.name }}
To check if the current user is in any of the groups selected:
{% if entry.userGroupFieldHandle.inGroup(currentUser) %}
<h1>User can access</h1>
{% endif %}
To check if the current user can access something based on the group selection:
{% if entry.userGroupFieldHandle.canAccess(currentUser) %}
<h1>User can access</h1>
{% endif %}
Note: This check is always true for admins.
You can also query a User Group Field based on the uid
of a user.
{% set userGroup = craft.app.userGroups.getGroupByHandle('myUserGroup') %}
{% set entries = craft.entries.section('blog').userGroupFieldHandle(userGroup.uid).all() %}