ulgm_buy_courses_qry_args
Filters the query arguments used for retrieving courses when a user purchases them, allowing modification of the course selection criteria.
add_filter( 'ulgm_buy_courses_qry_args', $callback, 10, 1 );
Description
Filter the query arguments used for fetching courses displayed on the "Buy Courses" page. Developers can modify post type, ordering, pagination, and taxonomy queries. This hook is applied before the courses are retrieved, allowing for custom filtering and sorting of purchasable courses.
Usage
add_filter( 'ulgm_buy_courses_qry_args', 'your_function_name', 10, 1 );
Parameters
-
$tax_query_fragment(mixed) - This parameter contains an array of arguments that will be used to construct a WordPress query for fetching courses.
Return Value
The filtered value.
Examples
<?php
/**
* Example of filtering the 'ulgm_buy_courses_qry_args' hook.
*
* This filter allows modification of the arguments used to query courses
* for the "Buy Courses" section. In this example, we're adding a condition
* to only include courses that are in stock.
*/
add_filter(
'ulgm_buy_courses_qry_args',
function ( $query_args ) {
// Ensure that 'tax_query' is set and is an array before proceeding.
if ( isset( $query_args['tax_query'] ) && is_array( $query_args['tax_query'] ) ) {
// Add a condition to the existing tax query to check for stock status.
// This assumes 'product_status' is a custom taxonomy or a meta field
// that can be queried via tax_query. A more realistic scenario might
// involve meta_query for stock, but adhering to the provided $tax_query_fragment.
// If 'product_status' isn't a taxonomy, this part would need adjustment.
// For demonstration, let's assume a taxonomy 'course_availability' with term 'in-stock'.
$stock_query_fragment = array(
'taxonomy' => 'course_availability', // Replace with your actual taxonomy name for stock status
'field' => 'slug',
'terms' => 'in-stock', // Replace with the term slug that indicates a course is in stock
);
// Check if the relation is already set, otherwise add it.
if ( ! isset( $query_args['tax_query']['relation'] ) ) {
$query_args['tax_query']['relation'] = 'AND';
}
// Add the stock query fragment to the existing tax query.
$query_args['tax_query'][] = $stock_query_fragment;
} else {
// If tax_query is not set, initialize it and add the stock query.
$query_args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'course_availability', // Replace with your actual taxonomy name for stock status
'field' => 'slug',
'terms' => 'in-stock', // Replace with the term slug that indicates a course is in stock
),
);
}
// You could also modify other arguments like posts_per_page, orderby, etc.
// For example, to always sort by price:
// $query_args['orderby'] = 'meta_value_num';
// $query_args['meta_key'] = '_price'; // Assuming WooCommerce products and _price meta
return $query_args;
},
10, // Priority
1 // 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/templates/frontend-uo_groups_buy_courses.php:135
}
// if any of the cats or tags are set, add the relation parameter.
if ( count( $tax_query_fragment ) > 1 ) {
$tax_query_fragment = array_merge( array( 'relation' => 'AND' ), $tax_query_fragment );
}
$args = apply_filters(
'ulgm_buy_courses_qry_args',
array(
'post_type' => 'product',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 8888,
'tax_query' => $tax_query_fragment,