• Resolved athos1

    (@athos1)


    Hello,

    The plugin documentation states that all users within a group can be called using

    $group = new Groups_Group( $group_id );
    $users = $group->users;

    Is there a way to create a further subset within a Group, for example, all Users in a Group with the same “user_registered” value from wp_users table (so if I have Users in Group 1 that were created on 1/1/2022 and on 2/2/2022 I could identify those two as subsets but without saying the specific date, just having all Users that have a matching value for that field be in the same subset)?

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi there,

    In this case you might perhaps utilize the group hierarchy and once you identify the subsets, create(if necessary) those sub-groups and add these users to each respective sub-group. Each these sub-groups should have the same parent group, the one you’re looking into for subsets.

    Also worth mentioning is the moment you wish to trigger that, on user creation or when a user is added to a group.

    Useful resources:
    https://developer.www.ads-software.com/reference/hooks/user_register/
    groups_created_user_group
    https://github.com/itthinx/groups/blob/master/lib/core/class-groups-group.php#L255
    https://github.com/itthinx/groups/blob/master/lib/core/class-groups-user-group.php#L94

    Kind regards,
    George

    Plugin Author Kento

    (@proaktion)

    I would simply approach this with an intersection:

    1. Get all user IDs in that group:

    $group = new Groups_Group( $group_id );
    $group_user_ids = $group->user_ids;

    2. Get users matching the desired time frame (*):

    $period_user_ids = get_users(
    	array(
            'fields' => 'ID',
            'date_query' => array(
                'compare' => 'BETWEEN',
                'day'     => [1,2],
                'month'   => [1,2],
                'year'    => [2022,2022]
            )
        )
    );

    3. Intersect :

    $user_ids = array_intersect( $group_user_ids, $period_user_ids );

    Which will give you the set of users matching both constraints.

    (*) If you want to do the direct query instead of using get_users() with a date_query, you could do:

    global $wpdb;
    $user_query = $wpdb->prepare(
        "SELECT ID FROM $wpdb->users WHERE user_registered >= %s AND user_registered <= %s",
        '2022-01-01 00:00:00',
        '2022-02-02 23:59:59'
    );
    $period_user_ids = $wpdb->get_col( $user_query );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Search for a subset within a Group’ is closed to new replies.