WP Learndash 插件,获取与课程和课程相关的用户数据

WP Learndash plugin, get userdata related to courses and lessons

您好,我正在使用 learndash Wordpress 插件。我想获取与用户相关的数据,即他注册了多少课程以及完成了多少课程。有没有办法检查这个? learndash 是否为此提供任何解决方案,还是我应该自己查询数据?

感谢任何帮助。提前致谢。 如果需要,请询问更多详细信息。

您可以 return 使用 wp_query 的任何内容。试试这个:

function wpso49370180_get_course_name($courseid) {
global $wpdb;
$user_id = the_author_meta( 'ID' ); //alt method below
$query_course = "SELECT post_title 
                 FROM wp_posts 
                 WHERE post_type = 'sfwd-courses'   
                 AND post_status NOT IN ( 'trash','auto-draft','inherit' ) 
                 AND post_author='$user_id' LIMIT 10";

      return $wpdb->get_var($query_course);
}

您需要了解 user_id 或从 post(sfwd-quiz、sfwd-course、sfwd-lesson)中获取它 - 见下文。

您想要的数据可以是全部 (*) 如果您想要 post_type 表中没有的更深层次的数据,则必须执行 meta_query。

/**
 * Gets the author of the specified post. Can also be used inside the loop
 * to get the ID of the author of the current post, by not passing a post ID.
 * Outside the loop you must pass a post ID.
 *
 * @param int $post_id ID of post
 * @return int ID of post author
*/
function wpso49370180_get_author( $post_id = 0 ){
    $post = get_post( $post_id );
        return $post->post_author;
}

您可以使用以下方法获取当前用户当前是否注册的所有课程:

learndash_user_get_enrolled_courses(get_current_user_id())

我发现所有内容都存储在 table learndash_user_activity 中,您可以查询 table 以获取用户统计信息。

例如,我使用以下查询获取给定课程的 in-progress 用户列表:

public static function get_inprogress_users_for_course( $course_id )
{
    global $wpdb;
    
    if( empty( $course_id ) ) return [];
                                    
    $results = $wpdb->get_results( "SELECT `user_id` FROM `" . $wpdb->prefix . "learndash_user_activity` "
                                    ."WHERE `course_id` = '" . intval( $course_id ) . "' "
                                    ."AND `activity_type` = 'lesson' "
                                    ."GROUP BY `user_id`" );
                                    
    return $results;
}

以同样的方式,您可以在查询中获取所有曾经注册过将 activity_type 从 'lesson' 更改为 'course' 的课程的用户 ID,如果您想要要仅获得用户注册的课程,您可以将 user_id 添加到查询中,如下所示:

public static function get_courses_for_user( $user_id )
{
    global $wpdb;
    
    if( empty( $course_id ) ) return [];
                                    
    $results = $wpdb->get_results( "SELECT * FROM `" . $wpdb->prefix . "learndash_user_activity` "
                                    ."WHERE `user_id` = '" . intval( $user_id ) . "' "
                                    ."AND `activity_type` = 'course' "
                                    ."GROUP BY `course_id`" );
                                    
    return $results;
}

我知道这不是您要搜索的内容,但它仍然有用。