How to fetch enrolled courses from Learndash in Elementor Loop Grid?

39 Views Asked by At

I'm trying to fetch the enrolled courses for the user in an elementor loop grid but it is throwing error:

"PHP Fatal error: Allowed memory size of 6340739072 bytes exhausted (tried to allocate 20480 bytes) in /www/be10x_939/public/wp-content/plugins/elementor-pro/modules/notes/user/capabilities.php on line 37".

Here's the code:

// Elementor Query for Learndash
function get_learndash_enrolled_courses_query() {
    if (is_user_logged_in()) {
        $user_id = get_current_user_id();
        // error_log($user_id);
        if (function_exists('learndash_user_get_enrolled_courses')) {
        // Function exists, proceed with your code
            $enrolled_courses = learndash_user_get_enrolled_courses($user_id, array(), false);
            error_log($enrolled_courses);
            return new WP_Query([
                'post_type' => 'sfwd-courses',
                'post__in' => $enrolled_courses,
            ]);
        } else {
            error_log('learndash_user_get_enrolled_courses function not found');
        }
    }
    return null;
}
add_action('elementor/query/aryn_learndash_enrolled_courses', function($query) {
    $custom_query = get_learndash_enrolled_courses_query();
    
    // Check if courses are found and modify the query accordingly
    if (!empty($enrolled_courses)) {
        error_log($custom_query);
        if ($custom_query) {
            $query->set('post__in', $custom_query->posts);
            $query->set('post_type', 'sfwd-courses');
        }
    }

});

I tried using multiple codes, but this was the most optimized but, it did not work. I'm expecting the function to return the courses for each user using the ELementor widget.

0

There are 0 best solutions below