LLMS_Course::get_student_count( boolean $skip_cache = false )

Retrieve the number of enrolled students in the course


Description Description

The cached value is calculated in the LLMS_Processor_Course_Data background processor.

If, for whatever reason, it’s not found, it will be calculated on demand and saved for later use.


Top ↑

Parameters Parameters

$skip_cache

(boolean) (Optional) Default: false. Whether or not to bypass the cache. If true, bypasses the cache.

Default value: false


Top ↑

Return Return

(int)


Top ↑

Source Source

File: includes/models/model.llms.course.php

	public function get_student_count( $skip_cache = false ) {

		$count = ! $skip_cache ? $this->get( 'enrolled_students' ) : false;

		/**
		 * Query enrolled students when `$skip_cache=true` or when there's no stored meta data.
		 *
		 * The second condition is necessary to disambiguate between a cached `0` and a `0` that's
		 * returned as the default value when the metadata doesn't exist.
		 */
		if ( false === $count || ! isset( $this->enrolled_students ) ) {

			$query = new LLMS_Student_Query(
				array(
					'post_id'  => $this->get( 'id' ),
					'statuses' => array( 'enrolled' ),
					'per_page' => 1,
					'sort'     => array(
						'id' => 'ASC',
					),
				)
			);

			$count = $query->get_found_results();

			// Cache result for later use.
			$this->set( 'enrolled_students', $count );

		}

		/**
		 * Filter the number of actively enrolled students in the course
		 *
		 * @since 4.12.0
		 *
		 * @param int         $count  Number of students enrolled in the course.
		 * @param LLMS_Course $course Instance of the course object.
		 */
		$count = apply_filters( 'llms_course_get_student_count', $count, $this );

		return absint( $count );

	}


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Don't access LLMS_Student_Query properties directly.
4.12.0 Use cached value where possible.
3.15.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.