LearnDash: Students Column on the Courses Screen
Adds a "Students" column to the LearnDash Courses list showing how many users are enrolled in each course, cached for speed.
Opens WPIDE on
Code
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
return;
}
add_filter( 'manage_sfwd-courses_posts_columns', function ( $columns ) {
$columns['wpide_ld_students'] = 'Students';
return $columns;
} );
add_action( 'manage_sfwd-courses_posts_custom_column', function ( $column, $course_id ) {
if ( 'wpide_ld_students' !== $column ) {
return;
}
$key = 'wpide_ld_students_' . $course_id;
$count = get_transient( $key );
if ( false === $count ) {
$query = new WP_User_Query( array(
'fields' => 'ID',
'number' => 1,
'count_total' => true,
'meta_key' => 'course_' . (int) $course_id . '_access_from',
'meta_compare' => 'EXISTS',
) );
$count = $query->get_total();
set_transient( $key, $count, 6 * HOUR_IN_SECONDS );
}
echo esc_html( number_format_i18n( (int) $count ) );
}, 10, 2 );
add_action( 'learndash_update_course_access', function ( $user_id, $course_id ) {
delete_transient( 'wpide_ld_students_' . (int) $course_id );
}, 10, 2 );
Notes
Counts direct enrollments; students who only get access through a group, or courses set to "Open", are not included. Counts refresh every 6 hours.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.