LMS & eLearning

Snippet: Add Instructor Name and Student Count to Course Grid Cards

Show a real instructor instead of the post author and a student count that includes group members on LearnDash Course Grid cards, using learndash_course_grid_author and learndash_course_grid_template_post_attributes.

Snippet: Add Instructor Name and Student Count to Course Grid Cards

Course Grid cards already show an author line and a student count, but both come from data that is often wrong for a training business. The author is the WordPress user who created the course post, usually an administrator rather than the instructor, and the student count only counts direct enrollments, so a course sold to companies through groups shows a handful of students when hundreds are taking it. LearnDash Course Grid 2.0.9 exposes both values through filters, and this snippet replaces them with values from your own fields.

Where the card gets its data

Every card is rendered from an array built by learndash_course_grid_prepare_template_post_attributes(). Two entries matter here:

  • author, an array with name and avatar built from the post author and passed through learndash_course_grid_author with the post ID and author ID.
  • students, the number returned by learndash_course_grid_count_students(), which counts user meta rows named course_{id}_access_from for courses or learndash_group_users_{id} for groups and caches the result in the object cache for an hour.

The whole array then passes through learndash_course_grid_template_post_attributes with the post object, which is where you can rewrite any value. The cards print the author when the post_meta attribute is on: grid-1, grid-3 and list-1 show "By name" with an avatar on grid-3 and list-1, grid-2 shows avatar and name, and list-2 shows the name. The student count appears in the meta row of the same cards. Our guide to showing price, progress and student count describes the default display; this article changes the values behind it.

Snippet 1: show an instructor from a custom field

Store the instructor's user ID in a custom field on the course, here _wwc_instructor_id, through a meta box, a custom fields plugin or an import. Then swap the author:

<?php
/**
 * Replace the Course Grid author with the instructor saved on the course.
 *
 * @param array $author    Array with 'name' and 'avatar' keys.
 * @param int   $post_id   Course ID.
 * @param int   $author_id WordPress post author ID.
 *
 * @return array
 */
function wwc_course_grid_instructor( $author, $post_id, $author_id ) {
    $instructor_id = (int) get_post_meta( $post_id, '_wwc_instructor_id', true );

    if ( $instructor_id <= 0 ) {
        return $author;
    }

    $instructor = get_user_by( 'ID', $instructor_id );

    if ( ! $instructor ) {
        return $author;
    }

    return [
        'name'   => $instructor->display_name,
        'avatar' => get_avatar_url( $instructor_id, [ 'size' => 96 ] ),
    ];
}
add_filter( 'learndash_course_grid_author', 'wwc_course_grid_instructor', 10, 3 );

Keep the two keys the templates expect. If the instructor is not a WordPress user, for example a guest lecturer, store a name and an image URL in two fields and return those instead; the cards only read the array.

Snippet 2: hide the author line on some courses

Returning an empty array removes the author block on cards that check for it, which is all of them except grid-2 and grid-3, which test the variable loosely and would print an empty avatar. For those two, return a name of an empty string and handle it in a template override, or use the class filter approach below.

<?php
add_filter(
    'learndash_course_grid_author',
    function ( $author, $post_id, $author_id ) {
        if ( has_term( 'self-paced', 'ld_course_category', $post_id ) ) {
            return [];
        }

        return $author;
    },
    10,
    3
);

Snippet 3: count group members in the student total

The built-in count ignores learners who have the course through a group. LearnDash core can list the users who have access from any source, so replace the number:

<?php
/**
 * Count direct and group-enrolled users for the Course Grid student total.
 *
 * @param array   $atts Card attributes.
 * @param WP_Post $post The course post.
 *
 * @return array
 */
function wwc_course_grid_total_students( $atts, $post ) {
    if ( 'sfwd-courses' !== $post->post_type ) {
        return $atts;
    }

    $cache_key = 'wwc_students_' . $post->ID;
    $total     = wp_cache_get( $cache_key, 'wwc' );

    if ( false === $total ) {
        $users = learndash_get_users_for_course( $post->ID, [], false );
        $total = is_a( $users, 'WP_User_Query' ) ? (int) $users->get_total() : 0;
        wp_cache_set( $cache_key, $total, 'wwc', HOUR_IN_SECONDS );
    }

    $atts['students'] = $total;

    return $atts;
}
add_filter( 'learndash_course_grid_template_post_attributes', 'wwc_course_grid_total_students', 10, 2 );

learndash_get_users_for_course() with the third argument set to false includes users who have access through groups and returns a WP_User_Query. It is heavier than the plugin's single COUNT query, which is why the result is cached for an hour, matching the plugin's own cache period. On sites with tens of thousands of learners, run the count in a scheduled job, store it in post meta and read the meta here instead.

Snippet 4: show a marketing number instead

Some schools prefer to show a rounded or lifetime figure. Store it in a field and let it override the live count when present:

<?php
add_filter(
    'learndash_course_grid_template_post_attributes',
    function ( $atts, $post ) {
        $override = get_post_meta( $post->ID, '_wwc_students_display', true );

        if ( '' !== $override ) {
            $atts['students'] = $override;
        }

        return $atts;
    },
    10,
    2
);

Because the cards print the value as given, a string such as "2,500+" works.

Adding a field that does not exist yet

The same attributes filter can add new keys, such as $atts['cohort_start'], but the stock cards will not print a key they do not know about. For new fields you need a card template override or a custom card that outputs them; our guide to building a custom Course Grid card walks through registering one. Replacing existing values, as in this article, needs no template changes.

Performance notes

The author filter costs one user lookup per card, which is negligible. The student count is the expensive part: the plugin's own COUNT query runs once per card per hour when a persistent object cache such as Redis or Memcached is active, and on every page load without one, because wp_cache_set() only lasts for the request in that case. The group-aware replacement in snippet 3 is heavier still. On a catalog with dozens of cards and no persistent cache, move the count into a scheduled job that writes a post meta value, and read that meta in the filter, so that rendering a grid never queries user tables.

Where the filters apply

Both filters run for the shortcode, the block, AJAX pagination and the legacy v1 card, so the instructor and count stay consistent across Load More pages. They do not affect the LearnDash course page itself or other listings. If a card template has been overridden in the theme and no longer prints the author or student sections, the filters still run but nothing shows; check the override first.

When to get help

These snippets cover one instructor per course and a single count. Multi-instructor courses, instructor profile pages linked from the card, ratings pulled from a review plugin and counts that must match a reporting system are small projects that combine these filters with a custom card, which is routine work for our LearnDash services team. A LearnDash developer can usually add an instructor field, meta box and card changes in a day; the full hook list is in the Course Grid hooks reference.

Frequently asked questions

Why does the author line show an administrator's name?

Because the card uses the WordPress post author, which is whoever created the course. Either change the post author on the course, or use the author filter above to show an instructor field.

Why is the student count lower than my enrollments?

The plugin counts only direct enrollments stored as user meta; learners who have the course through a group are not counted. Snippet 3 replaces the number with a count that includes them.

Can I show the instructor on the course page too?

Not with these filters, which are Course Grid only. Output the same custom field in your course template or through a LearnDash template override.

How often does the student count update?

The plugin caches each count for an hour in the object cache; without a persistent object cache it recalculates on every page load. The snippets use the same one-hour period.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

· Developers and project managers

Articles written and reviewed by the developers and project managers at WorldWin Coder, who build and maintain websites, web applications and mobile apps on WordPress, Moodle, PHP, Laravel, CodeIgniter and Flutter.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.