Filter uncanny-learndash-groups

lesson_group_dropdown

Filters the arguments used for retrieving lesson groups, allowing modification of the dropdown query based on user and existing posts.

add_filter( 'lesson_group_dropdown', $callback, 10, 3 );

Description

Filters the arguments used to query lesson groups for dropdowns. Developers can modify the `$args` array to alter the groups retrieved, add custom queries, or change ordering. The hook fires before groups are fetched for the dropdown, allowing comprehensive control over the displayed groups.


Usage

add_filter( 'lesson_group_dropdown', 'your_function_name', 10, 3 );

Parameters

$args (mixed)
This parameter contains an array of arguments used to query for lesson groups.
$user_id (mixed)
This parameter contains an array of arguments used to query for lesson group posts.
$posts_in (mixed)
This parameter contains the ID of the user for whom the lesson group dropdown is being generated.

Return Value

The filtered value.


Examples

<?php
/**
 * Example: Filter the 'lesson_group_dropdown' hook to exclude specific groups
 * based on a custom meta key.
 *
 * This example demonstrates how to modify the arguments passed to get_posts()
 * when building the group dropdown. It checks if the user is an administrator.
 * If so, it adds a meta query to exclude groups that have a custom meta key
 * 'exclude_from_reports' set to 'true'.
 */
add_filter( 'lesson_group_dropdown', function( $args, $user_id, $posts_in ) {

	// Only apply this logic if the user is an administrator.
	if ( user_can( $user_id, 'manage_options' ) ) {
		// Ensure the 'meta_query' element exists or initialize it.
		if ( ! isset( $args['meta_query'] ) || ! is_array( $args['meta_query'] ) ) {
			$args['meta_query'] = array();
		}

		// Add a meta query to exclude groups where 'exclude_from_reports' is 'true'.
		$args['meta_query'][] = array(
			'key'     => 'exclude_from_reports',
			'value'   => 'true',
			'compare' => '!=', // We want to include groups that DO NOT have this meta key set to 'true'
		);
	}

	// Always return the modified (or original) arguments.
	return $args;

}, 10, 3 ); // Priority 10, accepts 3 arguments: $args, $user_id, $posts_in

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/classes/reports/group-assignments.php:504
src/classes/reports/group-essays.php:445

'post_type'      => 'groups',
			'post__in'       => $posts_in,
			'posts_per_page' => 9999,
			'orderby'        => 'title',
			'order'          => 'ASC',
		);

		$args = apply_filters( 'lesson_group_dropdown', $args, $user_id, $posts_in );

		$group_post_objects = get_posts( $args );

		$drop_down['groups'] = '<option value="0">' . __( 'Select Group', 'uncanny-learndash-groups' ) . '</option>';
		//$drop_down['groups'] = '';
		$drop_down['lessons_objects'] = array();

Scroll to Top