uo_courses_shortcode_get_posts
Filters the arguments used to retrieve posts for the courses shortcode.
add_filter( 'uo_courses_shortcode_get_posts', $callback, 10, 3 );
Description
Filters the post query results for the `uo_courses_shortcode`. Developers can modify the array of `WP_Post` objects returned by `get_posts()` to alter the displayed courses. This hook fires after the initial post query has been executed.
Usage
add_filter( 'uo_courses_shortcode_get_posts', 'your_function_name', 10, 3 );
Parameters
-
$args(mixed) - This parameter is used to build the arguments that will be passed to the `get_posts` function, controlling which posts are retrieved.
-
$atts(mixed) - This parameter contains an array of arguments used to query posts for the courses shortcode.
-
$_GET(mixed) - This parameter holds the attributes passed to the shortcode that generates the course listing.
Return Value
The filtered value.
Examples
// Example: Modify the retrieved courses to exclude courses from a specific category.
add_filter( 'uo_courses_shortcode_get_posts', function( $posts, $atts, $get_data ) {
// Check if we have posts to process and if a specific category ID to exclude is provided.
if ( ! empty( $posts ) && isset( $get_data['exclude_category'] ) && is_numeric( $get_data['exclude_category'] ) ) {
$category_to_exclude_id = intval( $get_data['exclude_category'] );
$filtered_posts = array();
// Iterate through the retrieved posts and keep only those not in the excluded category.
foreach ( $posts as $post ) {
if ( ! has_term( $category_to_exclude_id, 'course_category', $post->ID ) ) { // Assuming 'course_category' is the taxonomy slug
$filtered_posts[] = $post;
}
}
return $filtered_posts;
}
// If no modification is needed, return the original posts.
return $posts;
}, 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:370
$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'];