I needed to do something similar to what @multimastery was looking for. I believe I figured out how to do it, so I wanted to share in case it would help someone else.
I created a shortcode that displays all of the PAGES that the currently logged in user has access to. This includes the pages for ALL groups that the user is associated with. It verifies that the user is logged in and is associated with at least one group before listing the pages. It still needs some polish and verification that the Groups plugin is active, but it’s a good start.
function custom_list_pages_for_users_groups()
{
// is the current user logged in?
$user_id = get_current_user_id();
if($user_id == 0)
return '';
// get the user object
$user = new Groups_User($user_id);
if(empty($user))
return '';
// get the groups that the user is part of
$group_ids = (is_array($user->group_ids_deep)) ? $user->group_ids_deep : [];
if(empty($group_ids))
return '';
// get the pages that the groups the user is part of has access to
$pages = Groups_Post_Access::get_pages(get_posts([
'posts_per_page' => -1,
'post_type' => 'page',
'meta_key' => 'groups-read',
'meta_value' => $group_ids,
'meta_compare' => 'IN',
'orderby' => 'title',
'order' => 'ASC'])
);
if(empty($pages))
return '<p>You do not have access to any content at this time.</p>';
$html = '<ul>';
foreach ($pages as $post)
$html .= '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a></li>';
$html .= '</ul>';
return $html;
}
add_shortcode('groups_list_pages_for_user', 'custom_list_pages_for_users_groups');
USAGE:
[groups_list_pages_for_user]
I thought there might be a need to expand on this and display the PAGES that a single group has access to, so I created a second shortcode to do that as well. It verifies that the user is logged in, that the group specified exists, and that the user is part of the specified group before listing the pages. Like the first one, this one could use some polish as well.
function custom_list_pages_for_group($attr)
{
$attributes = shortcode_atts(['group' => ''], $attr);
// is the current user logged in?
$user_id = get_current_user_id();
if($user_id == 0)
return '';
// make sure a group was specified
if(empty($attributes['group']))
return '';
// find the group specified and make sure it exists
$group = Groups_Group::read_by_name($attributes['group']);
if(!$group)
return '';
// get the user object
$user = new Groups_User($user_id);
if(empty($user))
return '';
// make sure the user is part of the specified group
$user_group_ids = (is_array($user->group_ids_deep)) ? $user->group_ids_deep : [];
if(!in_array($group->group_id, $user_group_ids))
return '';
// get the pages the group has access to
$pages = Groups_Post_Access::get_pages(get_posts([
'posts_per_page' => -1,
'post_type' => 'page',
'meta_key' => 'groups-read',
'meta_value' => [$group->group_id],
'meta_compare' => 'IN',
'orderby' => 'title',
'order' => 'ASC']
)
);
if(empty($pages))
return '<p>This group does not have access to any content at this time.</p>';
$html = '<ul>';
foreach ($pages as $post)
$html .= '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a></li>';
$html .= '</ul>';
return $html;
}
add_shortcode('groups_list_pages_for_group', 'custom_list_pages_for_group');
USAGE:
[groups_list_pages_for_group group="Group Name"]