Template::course_query_archive( $query )


Source

File: classes/Template.php

	public function course_query_archive($query){

		$courses_per_page = dozent_courses_per_page();
		$search_query = get_search_query();

		if ( is_dozent_course_archive_page() ) {
			$paged        = dozent_current_page();

			query_posts( [
				'post_type'      => 'dozent_course',
				'paged'          => $paged,
				's'              => $search_query,
				'posts_per_page' => $courses_per_page
			] );
		}

		if ( is_dozent_course_archive() ){

			$query->set('posts_per_page', $courses_per_page);

			$sort_by = dozent_input_text( 'sort_by' );
			if ( ! $sort_by ){
				$sort_by = 'relevance';
			}

			switch ($sort_by){

				case 'most-reviewed':
					$query->set('orderby', 'meta_value_num');
					$query->set('meta_key', '_rating_count');
					$query->set('order', 'desc');
					break;

				case 'highest-rated':
					$query->set('orderby', 'meta_value_num');
					$query->set('meta_key', '_rating_avg');
					$query->set('order', 'desc');
					break;
				case 'newest-first':
				case 'relevance':
					$query->set('orderby', 'ID');
					$query->set('order', 'desc');
					break;
				case 'oldest-first':
					$query->set('orderby', 'ID');
					$query->set('order', 'asc');
					break;
				case 'course-title-az':
					$query->set('orderby', 'post_title');
					$query->set('order', 'asc');
					break;
				case 'course-title-za':
					$query->set('orderby', 'post_title');
					$query->set('order', 'desc');
					break;
			}
		}

		/**
		 * Filter by level
		 */
		$levels = dozent_input_array_field( 'level' );

		if ( dozent_count( $levels ) && ! in_array( 'all', $levels ) ) {

			$level_query[] = [
				'key'     => '_difficulty_level',
				'value'   => $levels,
				'compare' => 'IN',
			];

			$query->set( 'meta_query', $level_query );
		}

		//Filter by Rating
		$rating = dozent_input_text( 'rating' );
		if ( $rating ) {
			$level_query[] = [
				'key'     => '_rating_avg',
				'value'   => $rating,
				'compare' => '>=',
			];
			$query->set( 'meta_query', $level_query );
		}
		
		//Filter by Video Duration
		$video_duration = dozent_input_text( 'video_duration' );

		if ( $video_duration ){
			
			if ($video_duration === '0_2'){
				$durationStart = 0;
				$durationEnd = (60 * 60 * 3) - 1; //02:59:59

				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => array( $durationStart, $durationEnd ),
					'type'    => 'numeric',
					'compare' => 'BETWEEN',
				];
				$query->set( 'meta_query', $level_query );

			}elseif ($video_duration === '3_5'){
				$durationStart = (60 * 60 * 3) ;
				$durationEnd = (60 * 60 * 6) -1;

				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => array( $durationStart, $durationEnd ),
					'type'    => 'numeric',
					'compare' => 'BETWEEN',
				];
				$query->set( 'meta_query', $level_query );

			}elseif ($video_duration === '6_10'){
				$durationStart = (60 * 60 * 6) ;
				$durationEnd = (60 * 60 * 11) -1;


				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => array( $durationStart, $durationEnd ),
					'type'    => 'numeric',
					'compare' => 'BETWEEN',
				];
				$query->set( 'meta_query', $level_query );

			}elseif ($video_duration === '11_20'){
				$durationStart = (60 * 60 * 11) ;
				$durationEnd = (60 * 60 * 21) -1;

				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => array( $durationStart, $durationEnd ),
					'type'    => 'numeric',
					'compare' => 'BETWEEN',
				];
				$query->set( 'meta_query', $level_query );

			}elseif ($video_duration === '21'){
				$durationStart = (60 * 60 * 21) ;

				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => $durationStart,
					'compare' => '>=',
				];
				$query->set( 'meta_query', $level_query );

			}

		}

	}