Learndash group access

From Lonsdale201/wp-agent-skills · View interactive version

Learndash group access is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Build or audit LearnDash group membership, group-course access, group leaders, and hierarchical group behavior. Use when code mentions ld_update_group_access, ld_update_course_group_access, learndash_get_users_group_ids, learndash_get_groups_user_ids, learndash_group_enrolled_courses, learndash_user

How to use this skill

Skill file

LearnDash group access

Use this when code needs to enroll users into groups, attach courses to groups,

manage group leaders, or answer "does this user get course access through a

group?"

LearnDash groups are not just taxonomy terms and not just post meta on a group.

In 5.1.9 the access model is three separate relationships:

Course access through a group exists only when the user is in the group and the

group is attached to the course, then the group product start/end rules allow

access.

When to use this skill

Trigger when ANY of these appears:

ld_update_leader_group_access(), learndash_is_user_in_group(),

learndash_user_group_enrolled_to_course(), or

learndash_get_users_group_ids().

group_{$group_id}_access_from,

learndash_group_{$group_id}_enrolled_at, or

learndash_group_leaders_{$group_id}.

learndash_group_{$group_id}_enrolled_at.

memberships, cohort sync, or REST endpoints.

Relationship map

User -> group

The direct user-group relation is stored on the user:

Use:

ld_update_group_access( $user_id, $group_id );        // grant
ld_update_group_access( $user_id, $group_id, true );  // revoke

Or use the model wrapper:

use LearnDash\Core\Models\Product;

$group_product = Product::find( $group_id );

if ( $group_product ) {
    $group_product->enroll( $user_id );
}

ld_update_group_access() creates access activity for courses currently

attached to the group, fires ld_added_group_access or

ld_removed_group_access, and clears user group/course transients.

Group -> course

The group-course relation is stored on the course post:

was added to the group.

reporting and should not be casually deleted.

Use:

ld_update_course_group_access( $course_id, $group_id );        // attach
ld_update_course_group_access( $course_id, $group_id, true );  // detach

For whole-list replacement, use the diff helpers:

They compute additions/removals and call ld_update_course_group_access() for

each delta.

Leader -> group

Group leaders are not the same as group members. A leader can administer a group

without being enrolled as a learner.

Use:

ld_update_leader_group_access( $leader_user_id, $group_id );
ld_update_leader_group_access( $leader_user_id, $group_id, true );

Read with:

Reading group access

Use the API that matches the question:

learndash_is_user_in_group( $user_id, $group_id ).

learndash_get_users_group_ids( $user_id ).

learndash_get_groups_user_ids( $group_id ) or

learndash_get_groups_users( $group_id ).

learndash_group_enrolled_courses( $group_id ).

learndash_get_course_groups( $course_id ).

learndash_user_group_enrolled_to_course( $user_id, $course_id ).

learndash_user_group_enrolled_to_course_from( $user_id, $course_id ).

Do not decide group course access by checking one meta key. The user must be in

the group, the group must be attached to the course, and the group Product

model must allow access.

Hierarchical groups

When hierarchical groups are enabled, learndash_get_users_group_ids() expands

the returned group IDs through validated child groups. That means:

contains the group.

for access decisions.

If the task is about reporting direct memberships, say so explicitly. If the

task is about access, use LearnDash's group APIs.

Managed Groups auto-enrollment in 5.1.8+

When groups_autoenroll_managed is enabled, a Group Leader's effective group

IDs are the union of:

leader.

Before 5.1.8, the managed-group branch replaced the explicit memberships. Do

not reproduce that old behavior in a custom portal or cache. Keep leader-to-

group administration and learner-to-group membership as separate relations,

then use learndash_get_users_group_ids() when effective access is required.

The effective list is cached for one minute in

learndash_user_groups_{$user_id}. Pass true as the second argument to

learndash_get_users_group_ids() when the caller requires a fresh result.

ld_update_group_access() clears this cache, but in 5.1.9

ld_update_leader_group_access() does not. After changing a leader assignment,

hierarchy, or the Managed Groups setting, explicitly invalidate the transient

with LDLMS_Transients::delete( 'learndash_user_groups_' . $user_id ) or use

the bypass until LearnDash's one-minute entry expires.

Cache and transient caveats

Some older function signatures still expose $bypass_transient, but LearnDash

5.x has removed or narrowed transient behavior in several group helpers:

the current implementation does not rely on the old cached query.

learndash_user_courses_{$user_id}.

Do not add custom long-lived caches unless the invalidation story covers user

membership changes, group-course changes, hierarchy changes, and group leader

changes.

Common mistakes to reject

directly instead of using the LearnDash helpers.

course postmeta.

access.

the user has no direct course_{$course_id}_access_from.

Cross-references

direct course enrollment, and access-from dates.

grant LearnDash group access.

courses, or user groups.

progress synchronization.

References

Validated against LearnDash LMS 5.1.9 local source:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.