Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Filter and Sort LearnDash Lessons

  • I’m trying to create a custom single post content template that shows a LearnDash course’s lessons. I’ve created custom code to filter the query, but it’s not filtering. I’m still getting used to the new GenerateBlocks GUI. I’m supposed to apply the class to the query block, right? Not the looper or loop item. I’ve applied the am-filter-course class to the query block but it has no effect. Can you assist?

    add_filter('am_generateblocks_query_loop_args', function($query_args, $attributes) {
        if (!empty($attributes['className']) && $attributes['className'] === 'am-filter-course') {
            if (is_singular('sfwd-courses')) {
                $course_id = get_the_ID();
    
                // filter for current course
                $query_args['meta_query'] = array(
                    array(
                        'key'     => 'course_id',
                        'value'   => $course_id,
                        'compare' => '=',
                    ),
                );
    
                // Add lesson ordering based on course_steps
                if (isset($query_args['post_type']) && $query_args['post_type'] === 'sfwd-lessons') {
                    // Get all course steps (lessons, topics, quizzes)
                    $course_steps = learndash_get_course_steps($course_id);
    
                    // Filter to include only lessons (sfwd-lessons)
                    $lesson_ids = array();
                    foreach ($course_steps as $step_id) {
                        if (get_post_type($step_id) === 'sfwd-lessons') {
                            $lesson_ids[] = $step_id;
                        }
                    }
    
                    // If there are lessons, modify the query to use post__in
                    if (!empty($lesson_ids)) {
                        $query_args['post__in'] = $lesson_ids;
                        $query_args['orderby'] = 'post__in'; // Respect the order of post__in
                        $query_args['posts_per_page'] = -1; // Ensure all lessons are included
                    }
                }
            }
        }
        return $query_args;
    }, 10, 2);
  • Hi there,

    It looks like you incorrectly prefixed the filter name with am_, which is why it isn’t working.

    Also, in the V2 Query block, make sure to use the correct filter name: generateblocks_query_wp_query_args

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.