В файле: content-archive-course. php
$default_order = apply_filters( 'thim_default_order_course_option', array(
'newly-published' => esc_html__( 'Newly published', 'eduma' ),
'alphabetical' => esc_html__( 'Alphabetical', 'eduma' ),
'most-members' => esc_html__( 'Most members', 'eduma' ),
'priceasc' => esc_html__( 'Prices: low to High', 'eduma' ),
'pricedesc' => esc_html__( 'Prices: high to low', 'eduma' ),
)) ;;
In you functions. php file добавьте этот хук:
/*Add new filter options to course archive page */
add_action( 'pre_get_posts', 'thim_course_order_query', 15);
function thim_course_order_query( $query ) {
if ( !$query->is_main_query() ) {
return;
}
if ( !is_post_type_archive( 'lp_course' ) && !is_tax( 'course_category' ) ) {
return;
}
// Sort
if ( isset( $_POST['course_orderby'] ) ) {
switch ( $_POST['course_orderby'] ) {
case 'alphabetical':
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
break;
case 'most-members':
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', 'thim_real_student_enrolled' );
$query->set( 'order', 'DESC' );
break;
case 'priceasc':
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', '_lp_price' );
$query->set( 'order', 'ASC' );
break;
case 'pricedesc':
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', '_lp_price' );
$query->set( 'order', 'DESC' );
break;
default:
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' );
}
}
// Pagination
if ( isset( $_POST['course_paged'] ) ) {
$query->set( 'paged', $_POST['course_paged'] );
}
// Filter by categories
if ( isset( $_POST['course_cate_filter'] ) && is_array( $_POST['course_cate_filter'] ) ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => 'course_category',
'field' => 'term_id',
'terms' => $_POST['course_cate_filter'],
)
) );
//$query->set( 'posts_per_page', - 1 );
}
// Filter by instructor
if ( isset( $_POST['course_instructor_filter'] ) && is_array( $_POST['course_instructor_filter'] ) ) {
$query->set( 'author__in', $_POST['course_instructor_filter'] );
}
// Filter by price
// TODO query courses has sale price
if ( isset( $_POST['course_price_filter'] ) ) {
switch ( $_POST['course_price_filter'] ) {
case 'free':
$query->set( 'meta_query', array(
array(
'key' => '_lp_price',
'compare' => 'NOT EXISTS'
)
) );
break;
case 'paid':
$query->set( 'meta_query', array(
array(
'key' => '_lp_price',
'compare' => 'EXISTS'
)
) );
break;
case 'all':
break;
default:
}
}
}