Browse: Home / Snippets /

Get the number of quiz attempts by a user_id for a specific quiz_id

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.

/* Get the number of attempt by a user_id for a specific quiz_id */
function get_user_quiz_attempts($user_id, $quiz_id = 0) {
	$attempts_count = 0;
	
	$usermeta = get_user_meta( $user_id, '_sfwd-quizzes', true );
	if (!empty( $usermeta ) ) {
		$usermeta = maybe_unserialize( $usermeta );

		if ( ! is_array( $usermeta ) ) { 
			$usermeta = array();
		}

		foreach ( $usermeta as $k => $v ) {
			if ( $v['quiz'] == $quiz_id ) { 
				$attempts_count++;
			} 
		}
	}
	
	return $attempts_count;
}