Filter uncanny-toolkit-pro

uo_courses_shortcode_args

Filters the arguments passed to the courses shortcode, allowing for customization before output.

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

Description

Filters the arguments used to retrieve courses via the `uo_courses` shortcode. This hook allows developers to modify query parameters, including taxonomy queries, before courses are fetched. It's useful for customizing course display based on custom criteria or external data.


Usage

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

Parameters

$args (mixed)
This parameter contains the arguments array used to query posts for the courses shortcode.
$atts (mixed)
This parameter contains the arguments array that will be used to query courses.
$_GET (mixed)
This parameter contains the attributes passed to the shortcode, which can be used to modify the query arguments.

Return Value

The filtered value.


Examples

/**
 * Modifies the query arguments for the 'uo_courses' shortcode to include
 * a specific course category if it's present in the URL query parameters.
 *
 * @param array $args    The current query arguments for the courses shortcode.
 * @param array $atts    The attributes passed to the 'uo_courses' shortcode.
 * @param array $_GET    The global $_GET superglobal array.
 * @return array The modified query arguments.
 */
function my_custom_course_query_args( $args, $atts, $_GET ) {

	// Check if a specific course category is passed via GET parameter 'course_category'.
	if ( isset( $_GET['course_category'] ) && ! empty( $_GET['course_category'] ) ) {
		$category_slug = sanitize_text_field( $_GET['course_category'] );

		// Ensure 'tax_query' is initialized if it doesn't exist.
		if ( ! isset( $args['tax_query'] ) || ! is_array( $args['tax_query'] ) ) {
			$args['tax_query'] = array();
		}

		// Add the 'tax_query' argument to filter by the course category.
		// We'll assume 'course_category' is a custom taxonomy slug for courses.
		$args['tax_query'][] = array(
			'taxonomy' => 'course_category', // Replace with your actual course category taxonomy slug
			'field'    => 'slug',
			'terms'    => $category_slug,
		);

		// If there's more than one tax query condition, set the relation.
		// The default is usually 'AND', but if we are adding a condition,
		// we might want to explicitly set it if it wasn't already set or to override.
		if ( count( $args['tax_query'] ) > 1 && ! isset( $args['tax_query']['relation'] ) ) {
			$args['tax_query']['relation'] = 'AND';
		}
	}

	return $args;
}
add_filter( 'uo_courses_shortcode_args', 'my_custom_course_query_args', 10, 3 );

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/show-all-courses.php:369

if( ! in_array( $taxonomy_relation, array('OR','AND'), true ) ) {
				$taxonomy_relation = 'OR';
			}

			$args['tax_query']['relation'] = $taxonomy_relation;
		}

		$args    = apply_filters( 'uo_courses_shortcode_args', $args, $atts, $_GET );
		$courses = apply_filters( 'uo_courses_shortcode_get_posts', get_posts( $args ), $atts, $_GET );

		$total_courses = count( $courses );
		$total         = 0;
		$cols          = $atts['cols'];
		$show          = $atts['limit'];
		$ignore        = $atts['ignore_default_sorting'];


Scroll to Top