Filter
uncanny-automator
uap_option_learndash_validate_groups
Filters LearnDash group query arguments to validate group data before querying.
add_filter( 'uap_option_learndash_validate_groups', $callback, 10, 1 );
Description
Filters the arguments used to query LearnDash groups when validating them within Uncanny Automator. Developers can modify query parameters like post IDs, order, or posts per page to customize group selection or validation logic. This hook fires before the group query is executed.
Usage
add_filter( 'uap_option_learndash_validate_groups', 'your_function_name', 10, 1 );
Parameters
-
$groups_query_args(mixed) - This parameter contains an array of group IDs used to query for existing groups.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the 'uap_option_learndash_validate_groups' filter hook.
*
* This example demonstrates how to modify the WordPress query arguments
* for retrieving LearnDash groups. It adds a custom meta query to only
* fetch groups that have a specific meta key set, effectively filtering
* them based on custom group attributes.
*
* @param array $groups_query_args The original query arguments for fetching LearnDash groups.
* @return array The modified query arguments.
*/
add_filter( 'uap_option_learndash_validate_groups', function( $groups_query_args ) {
// Ensure the 'meta_query' key exists or initialize it as an array.
if ( ! isset( $groups_query_args['meta_query'] ) || ! is_array( $groups_query_args['meta_query'] ) ) {
$groups_query_args['meta_query'] = array();
}
// Add a custom meta query to filter groups.
// For example, let's only fetch groups that have a meta key named 'custom_group_status'
// and its value is 'active'.
$groups_query_args['meta_query'][] = array(
'key' => 'custom_group_status',
'value' => 'active',
'compare' => '=',
);
// You could also modify existing query parameters if needed.
// For instance, to ensure only a specific set of groups are returned if 'post__in' is present.
// if ( isset( $groups_query_args['post__in'] ) && is_array( $groups_query_args['post__in'] ) ) {
// // Example: further filter the existing group IDs
// $filtered_group_ids = array_slice( $groups_query_args['post__in'], 0, 5 ); // Take only the first 5
// $groups_query_args['post__in'] = $filtered_group_ids;
// }
return $groups_query_args;
}, 10, 1 ); // 10 is the priority, 1 is the number of accepted 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/integrations/uncanny-groups/helpers/uncanny-groups-helpers.php:140
uncanny-automator-pro/src/integrations/learndash/helpers/learndash-pro-helpers.php:1468
public function learndash_validate_groups( $group_ids = array() ) {
if ( ( is_array( $group_ids ) ) && ( ! empty( $group_ids ) ) ) {
$groups_query_args = array(
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'post_type' => learndash_get_post_type_slug( 'group' ),
'fields' => 'ids',
'orderby' => 'title',
'order' => 'ASC',
'post__in' => $group_ids,
'posts_per_page' => -1,
'suppress_filters' => true,
);
$groups_query_args = apply_filters( 'uap_option_learndash_validate_groups', $groups_query_args );
$groups_query = new WP_Query( $groups_query_args );
if ( ( is_a( $groups_query, 'WP_Query' ) ) && ( property_exists( $groups_query, 'posts' ) ) ) {
return $groups_query->posts;
}
}
return array();
}