Filter uncanny-learndash-groups

quiz_group_dropdown

Filters the arguments and data used to build the quiz group dropdown menu.

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

Description

Filters the arguments used to query the 'groups' post type for the group quiz report dropdown. Developers can modify the query arguments to customize which groups are displayed, affecting the options available to users when generating group quiz reports.


Usage

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

Parameters

$args (mixed)
This parameter contains an array of arguments used to query for WordPress 'groups' post types, specifying criteria like post type, specific post IDs, and ordering.
$user_id (mixed)
This parameter is an array of arguments used to query WordPress posts of the 'groups' post type.
$posts_in (mixed)
This parameter contains the ID of the user for whom the quiz group dropdown is being generated.

Return Value

The filtered value.


Examples

<?php
/**
 * Filter to modify the arguments for fetching quiz groups,
 * for example, to add additional query parameters.
 *
 * @param array  $args      The original query arguments.
 * @param int    $user_id   The ID of the current user.
 * @param array  $posts_in  An array of post IDs to potentially filter by.
 * @return array Modified query arguments.
 */
add_filter( 'quiz_group_dropdown', function( $args, $user_id, $posts_in ) {

	// Example: Add a meta query to only include groups that have a specific quiz assigned.
	// This assumes a meta key named 'assigned_quiz_id' exists and stores the ID of a quiz.
	// We'll pretend $user_id is relevant here to show how it could be used,
	// though in this specific example, it's not directly used in the meta query.
	if ( isset( $user_id ) && ! empty( $user_id ) ) {
		// Let's say we want to filter groups based on quizzes the user has completed.
		// For demonstration, let's assume we have a way to get a list of quiz IDs
		// associated with the current user.
		// For this example, we'll hardcode a hypothetical quiz ID.
		$hypothetical_quiz_id = 123; // Replace with actual logic to get quiz IDs

		if ( $hypothetical_quiz_id ) {
			$args['meta_query'] = array(
				array(
					'key'     => 'assigned_quiz_id',
					'value'   => $hypothetical_quiz_id,
					'compare' => '=',
				),
			);
		}
	}

	// Example: If the $posts_in parameter is empty, we might want to exclude
	// certain groups based on another condition, or perhaps fetch all if it's empty.
	// For this example, if $posts_in is empty, we'll ensure we don't query at all
	// by setting posts_per_page to 0, which effectively returns no posts.
	if ( empty( $posts_in ) ) {
		$args['posts_per_page'] = 0;
	}

	return $args;
}, 10, 3 ); // Priority 10, accepts 3 arguments
?>

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-quiz-report.php:533

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

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

			$group_post_objects = new WP_Query( $args );
			if ( $group_post_objects->have_posts() ) {
				while ( $group_post_objects->have_posts() ) {
					$group_post_objects->the_post();
					$drop_down['groups'] .= '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>';
				}

Scroll to Top