BP_REST_Learndash_Courses_Endpoint::get_items( WP_REST_Request $request )
Retrieve courses.
Description
Parameters
- $request
-
(Required) Full details about the request.
Return
(WP_REST_Response) | WP_Error
Source
File: bp-integrations/learndash/classes/class-bp-rest-learndash-courses-endpoint.php
public function get_items( $request ) { $args = array( 'post_type' => 'sfwd-courses', 'order' => ( ! empty( $request['order'] ) ? $request['order'] : 'desc' ), 'orderby' => ( ! empty( $request['orderby'] ) ? $request['orderby'] : 'date' ), 'paged' => ( ! empty( $request['page'] ) ? $request['page'] : 1 ), 'posts_per_page' => ( ! empty( $request['per_page'] ) ? $request['per_page'] : 10 ), 'post_status' => ( ! empty( $request['status'] ) ? implode( ' ', $request['status'] ) : 'publish' ), ); if ( ! empty( $request['search'] ) ) { $args['s'] = sanitize_title( trim( $request['search'] ) ); } if ( ! empty( $request['author'] ) ) { $args['author'] = $request['author']; } if ( ! empty( $request['author_exclude'] ) ) { $args['author__not_in'] = $request['author_exclude']; } if ( ! empty( $request['exclude'] ) ) { $args['post__not_in'] = $request['exclude']; } if ( ! empty( $request['include'] ) ) { $args['post__in'] = $request['include']; } if ( ! empty( $request['offset'] ) ) { $args['offset'] = $request['offset']; } $args['date_query'] = array(); // Set before into date query. Date query must be specified as an array of an array. if ( isset( $request['before'] ) ) { $args['date_query'][0]['before'] = $request['before']; } // Set after into date query. Date query must be specified as an array of an array. if ( isset( $request['after'] ) ) { $args['date_query'][0]['after'] = $request['after']; } if ( ! empty( $request['course_category'] ) ) { if ( in_array( 0, $request['course_category'], true ) ) { $args['tax_query'][] = array( 'taxonomy' => 'ld_course_category', 'operator' => 'NOT EXISTS', ); $categories = array(); foreach ( $request['course_category'] as $k => $v ) { if ( ! empty( $v ) ) { $categories[] = $v; } } $request['course_category'] = $categories; unset( $categories ); } if ( ! empty( $request['course_category'] ) ) { $args['tax_query'][] = array( 'taxonomy' => 'ld_course_category', 'field' => 'term_id', 'terms' => $request['course_category'], 'include_children' => false, ); } } if ( ! empty( $request['course_category_exclude'] ) ) { $args['tax_query'][] = array( 'taxonomy' => 'ld_course_category', 'field' => 'term_id', 'terms' => $request['course_category_exclude'], 'include_children' => false, 'operator' => 'NOT IN', ); } if ( ! empty( $request['course_tag'] ) ) { $args['tax_query'][] = array( 'taxonomy' => 'ld_course_tag', 'field' => 'term_id', 'terms' => $request['course_tag'], 'include_children' => false, ); } if ( ! empty( $request['course_tag_exclude'] ) ) { $args['tax_query'][] = array( 'taxonomy' => 'ld_course_tag', 'field' => 'term_id', 'terms' => $request['course_tag_exclude'], 'include_children' => false, 'operator' => 'NOT IN', ); } if ( isset( $request['group_id'] ) && ! empty( $request['group_id'] ) ) { $group_id = bp_ld_sync( 'buddypress' )->helpers->getLearndashGroupId( $request['group_id'] ); $course_ids = learndash_group_enrolled_courses( $group_id ); $args['post__in'] = ! empty( $course_ids ) ? $course_ids : array( 0 ); unset( $args['author'] ); } /** * Filter the query arguments for the request. * * @param array $args Key value array of query var to query value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ $args = apply_filters( 'bp_rest_learndash_courses_get_items_query_args', $args, $request ); $courses_query = new WP_Query( $args ); $courses = $courses_query->posts; $retval = array(); if ( ! empty( $courses ) ) { foreach ( $courses as $course ) { $retval[] = $this->prepare_response_for_collection( $this->prepare_item_for_response( $course, $request ) ); } } $response = rest_ensure_response( $retval ); $response = bp_rest_response_add_total_headers( $response, $courses_query->found_posts, $args['posts_per_page'] ); /** * Fires after a list of courses is fetched via the REST API. * * @param array $courses Fetched Courses. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_learndash_courses_get_items', $courses, $response, $request ); return $response; }
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.