LLMS_Lesson::get_sibling_section_query( string $direction )

Performs a query to retrieve sibling lessons from the lesson’s adjacent section


Description Description

This will retrieve either the first lesson from the course’s next section or the last lesson from the course’s previous section.


Top ↑

Parameters Parameters

$direction

(string) (Required) Direction of navigation. Accepts either "prev" or "next".


Top ↑

Return Return

(false|int) WP_Post ID of the sibling lesson or false if one doesn't exist.


Top ↑

Source Source

File: includes/models/model.llms.lesson.php

870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
protected function get_sibling_section_query( $direction ) {
 
    $sibling_lesson = false;
    $curr_section   = $this->get_section();
 
    // Ensure we're not working with an orphan.
    if ( $curr_section ) {
 
        $curr_position = $curr_section->get( 'order' );
 
        // First cannot have a previous.
        if ( 1 === $curr_position && 'prev' === $direction ) {
            return false;
        }
 
        if ( 'next' === $direction ) {
            $sibling_position = $curr_position + 1;
            $order            = 'ASC';
        } elseif ( 'prev' === $direction ) {
            $sibling_position = $curr_position - 1;
            $order            = 'DESC';
        }
 
        $args = array(
            'post_type'      => 'section',
            'posts_per_page' => 1,
            'nopaging'       => true,
            'meta_key'       => '_llms_order', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
            'orderby'        => 'meta_value_num',
            'order'          => $order,
            'meta_query'     => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
                'relation' => 'AND',
                array(
                    'key'     => '_llms_parent_course',
                    'value'   => $this->get( 'parent_course' ),
                    'compare' => '=',
                ),
                array(
                    'key'     => '_llms_order',
                    'value'   => $sibling_position,
                    'compare' => '=',
                ),
            ),
        );
 
        /**
         * Filter the WP_Query arguments used to locate a sibling lesson from a sibling section for the specified lesson.
         *
         * @since 4.10.2
         *
         * @param array       $args      WP_Query arguments array.
         * @param string      $direction Navigation direction. Either "prev" or "next".
         * @param LLMS_Lesson $lesson    Current lesson object.
         */
        $args = apply_filters( 'llms_lesson_get_sibling_section_query_args', $args, $direction, $this );
 
        $sections = get_posts( $args );
 
        if ( ! empty( $sections ) ) {
            $sibling_section = llms_get_post( $sections[0]->ID );
            $lessons         = $sibling_section ? $sibling_section->get_lessons( 'posts' ) : array( false );
            $sibling_lesson  = 'next' === $direction ? reset( $lessons ) : end( $lessons );
        }
    }
 
    return $sibling_lesson instanceof WP_Post ? $sibling_lesson->ID : $sibling_lesson;
 
}


Top ↑

Changelog Changelog

Changelog
Version Description
5.7.0 Replaced the call to the deprecated LLMS_Section::get_order() method with LLMS_Section::get( 'order' ).
4.11.0 Fix PHP Notice when trying to retrieve next lesson from an empty section.
4.10.2 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.