Filter uncanny-learndash-groups

ulgm_group_courses_list_get_posts_vars

Filters the variables used to query courses for a group, allowing modification of post query arguments.

add_filter( 'ulgm_group_courses_list_get_posts_vars', $callback, 10, 2 );

Description

This filter allows modification of the query variables used to retrieve courses for a specific LearnDash group. Developers can use this hook to alter the sorting, ordering, or other parameters of the course query before it's executed. It fires after fetching group course order settings and before the `WP_Query` arguments are finalized.


Usage

add_filter( 'ulgm_group_courses_list_get_posts_vars', 'your_function_name', 10, 2 );

Parameters

$post_vars (mixed)
This parameter contains an array of variables used to query posts for a group's course list, which can be modified by this filter.
$group_id (mixed)
This parameter contains an array of variables used to query posts, which can be modified by the filter.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the ulgm_group_courses_list_get_posts_vars filter.
 *
 * This example demonstrates how to modify the query arguments to exclude
 * courses that have been completed by the current user within a specific group.
 */
add_filter( 'ulgm_group_courses_list_get_posts_vars', function( $post_vars, $group_id ) {

	// Check if the current user is logged in.
	if ( is_user_logged_in() ) {
		$current_user_id = get_current_user_id();

		// Get a list of courses completed by the current user in this group.
		// This assumes you have a way to fetch completed courses for a user within a group.
		// Replace 'get_completed_courses_for_user_in_group' with your actual function.
		$completed_courses = get_completed_courses_for_user_in_group( $current_user_id, $group_id );

		if ( ! empty( $completed_courses ) ) {
			// If there are completed courses, add them to the 'post__not_in' argument
			// to exclude them from the query results.
			$post_vars['post__not_in'] = $completed_courses;
		}
	}

	// Return the modified post variables.
	return $post_vars;
}, 10, 2 ); // 10 is the priority, 2 is the number of accepted arguments.

/**
 * Placeholder function to simulate fetching completed courses for a user in a group.
 * In a real scenario, you would query the WordPress database or use a plugin's API.
 *
 * @param int $user_id The ID of the user.
 * @param int $group_id The ID of the group.
 * @return array An array of course IDs that are completed by the user in the group.
 */
function get_completed_courses_for_user_in_group( $user_id, $group_id ) {
	// This is a mock implementation. Replace with actual logic.
	// For demonstration purposes, let's return a few course IDs if the user ID is odd.
	if ( $user_id % 2 !== 0 ) {
		return array( 101, 105, 112 ); // Example completed course IDs
	}
	return array();
}
?>

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/templates/frontend-uo_groups/users-table-actions.php:62

// Sort by group courses order settings.
			$ld_group_courses_order = learndash_get_group_courses_order( $group_id );
			if ( is_array( $ld_group_courses_order ) ) {
				$post_vars['orderby'] = ! empty( $ld_group_courses_order['orderby'] ) ? $ld_group_courses_order['orderby'] : $post_vars['orderby'];
				$post_vars['order']   = ! empty( $ld_group_courses_order['order'] ) ? $ld_group_courses_order['order'] : $post_vars['order'];
			}

			$post_vars = apply_filters( 'ulgm_group_courses_list_get_posts_vars', $post_vars, $group_id );

			$the_query = new WP_Query( $post_vars );

			// The Loop
			if ( $the_query->have_posts() ) {
				while ( $the_query->have_posts() ) {
					$the_query->the_post();


Scroll to Top