ulgm_group_courses_list_get_pre_posts_vars
Filters the query variables used to retrieve courses for a specific group before the query runs.
add_filter( 'ulgm_group_courses_list_get_pre_posts_vars', $callback, 10, 2 );
Description
Filters the arguments used to query courses for a specific group before the database query is executed. Developers can modify order, posts per page, and other query parameters. The `$post_vars` can be set to `null` to prevent the database query altogether.
Usage
add_filter( 'ulgm_group_courses_list_get_pre_posts_vars', 'your_function_name', 10, 2 );
Parameters
-
$post_vars(mixed) - This parameter contains an array of variables used to query posts, allowing modification before the database query is executed.
-
$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
/**
* Modify the query arguments for fetching group courses.
*
* This example demonstrates how to add an additional ordering parameter
* to the post query when fetching courses associated with a LearnDash group.
* It prioritizes the order defined in the group settings if available.
*
* @param array $post_vars The original query arguments for fetching posts.
* @param int $group_id The ID of the LearnDash group.
* @return array The modified query arguments.
*/
add_filter( 'ulgm_group_courses_list_get_pre_posts_vars', function( $post_vars, $group_id ) {
// Ensure $post_vars is an array, otherwise initialize it.
if ( ! is_array( $post_vars ) ) {
$post_vars = array();
}
// Fetch the group's course ordering settings.
$ld_group_courses_order = learndash_get_group_courses_order( $group_id );
// If group ordering settings are available and include an 'orderby' key,
// update the 'orderby' in our query arguments.
if ( is_array( $ld_group_courses_order ) && isset( $ld_group_courses_order['orderby'] ) && ! empty( $ld_group_courses_order['orderby'] ) ) {
$post_vars['orderby'] = $ld_group_courses_order['orderby'];
// If the order is also specified in the group settings, use that.
if ( isset( $ld_group_courses_order['order'] ) && ! empty( $ld_group_courses_order['order'] ) ) {
$post_vars['order'] = $ld_group_courses_order['order'];
}
} else {
// If no specific order is found in group settings, ensure a default is set.
// This might already be handled in the source context, but it's good
// practice to ensure a fallback.
if ( ! isset( $post_vars['orderby'] ) ) {
$post_vars['orderby'] = 'title'; // Default to ordering by course title
}
if ( ! isset( $post_vars['order'] ) ) {
$post_vars['order'] = 'ASC'; // Default to ascending order
}
}
// You could also add other custom logic here, for example:
// if ( current_user_can( 'manage_options' ) ) {
// $post_vars['meta_query'] = array(
// array(
// 'key' => '_custom_group_course_flag',
// 'value' => 'yes',
// 'compare' => '=',
// ),
// );
// }
return $post_vars;
}, 10, 2 );
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:51
'order' => 'ASC',
'posts_per_page' => 99999,
'nopaging' => true,
);
}
$courses = array();
$post_vars = apply_filters( 'ulgm_group_courses_list_get_pre_posts_vars', $post_vars, $group_id );
// Only query DB if applicable - Performance.
if ( null !== $post_vars ) {
// 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'];