• Resolved multimastery

    (@multimastery)


    Hello,

    How would I create a designated page so that a group member could easily view all the available content for their group. I have read the documentation several times and just don’t see anything that addresses this, unless I’m missing something. Seems like there would be a short code or something that would automatically populate a designated page with all of a particular group’s content.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Kento

    (@proaktion)

    I think it’s a good question but I’m not sure whether there is a universal solution that would fit into Groups as part of its core. Maybe it would make sense.

    If you want to list content that is restricted to a group, you could get_posts() with meta_key ‘groups-read’ and meta_value the group’s ID. I think it’s rather specific to a site but will be happy to hear suggestions for a generic solution.

    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"]

    Plugin Author Kento

    (@proaktion)

    Many thanks for posting your solution @beckyabsolute 🙂

    To check if group is active, you could use

    if ( !defined( 'GROUPS_CORE_VERSION' ) {
        return '';
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘View all content for that group?’ is closed to new replies.