LLMS_Lesson::get_sibling_lesson_query( string $direction )
Performs a query to retrieve a sibling lesson in the specified direction
Description Description
This method tries to locate a sibling lesson in the next or previous position.
It does not account for lessons in a sibling section. For example, if the lesson is the last lesson in a section this function will not locate the first lesson in the course’s next section. For this reason this function should not be relied upon alone.
Parameters Parameters
- $direction
-
(string) (Required) Direction of navigation. Accepts either "prev" or "next".
Return Return
(false|int) WP_Post ID of the sibling lesson or false if one doesn't exist.
Source Source
File: includes/models/model.llms.lesson.php
protected function get_sibling_lesson_query( $direction ) {
$curr_position = $this->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';
$comparator = '>=';
} elseif ( 'prev' === $direction ) {
$sibling_position = $curr_position - 1;
$order = 'DESC';
$comparator = '<=';
}
$args = array(
'posts_per_page' => 1,
'post_type' => 'lesson',
'nopaging' => true,
'post_status' => 'publish',
'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_section',
'value' => $this->get_parent_section(),
'compare' => '=',
),
array(
'key' => '_llms_order',
'value' => $sibling_position,
'compare' => $comparator,
'type' => 'numeric',
),
),
);
/**
* Filter the WP_Query arguments used to locate a sibling lesson 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_lesson_query_args', $args, $direction );
$lessons = get_posts( $args );
return empty( $lessons ) ? false : $lessons[0]->ID;
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 5.7.0 | Replaced the call to the deprecated LLMS_Lesson::get_order() method with LLMS_Lesson::get( 'order' ). |
| 4.10.2 | Introduced. |