page = absint( $page ); $this->per_page = $per_page; $this->skip_meta = $skip_meta; $args = [ 'posts_per_page' => $this->per_page, 'paged' => $this->page, 'post_type' => 'academy_lessons', ]; if ( ! empty( $author_id ) ) { $args['author'] = $author_id; } if ( ! empty( $search ) ) { $args['s'] = $search; } if ( ! empty( $status ) ) { $args['post_status'] = $status; } $by_meta = array_merge( $by_meta, apply_filters( 'academy/lesson/meta_query', [] ) ); if ( ! empty( $by_meta ) ) { $args['meta_query'] = $by_meta; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- meta/tax lookup the feature depends on; no cheaper equivalent } $this->query = new WP_Query( $args ); if ( $this->query->have_posts() ) { $this->lessons = (array) $this->query->posts; } else { $this->lessons = []; } $this->total = count( $this ); $this->total_pages = absint( $this->query->max_num_pages ); $this->offset = 0; $this->load_meta(); } public function getIterator(): ArrayIterator { return new ArrayIterator( $this->lessons ); } public function load_meta(): void { $ids = array_map( 'absint', array_column( $this->lessons, 'ID' ) ); if ( empty( $ids ) ) { $this->lessons = []; return; } if ( ! $this->skip_meta ) { // One query for all lessons, through the meta cache. update_meta_cache( 'post', $ids ); foreach ( $ids as $id ) { foreach ( (array) get_post_meta( $id ) as $meta_key => $meta_values ) { // Raw (still serialized) values, last row wins - same as the old direct query. $this->meta_data[ $id ][ $meta_key ] = end( $meta_values ); } } } $lessons = []; foreach ( $this->lessons as $lesson ) { $lesson->meta = $this->meta_data[ $lesson->ID ] ?? []; $lessons[] = new PostLesson( (array) $lesson, (array) $lesson->meta ); } $this->lessons = $lessons; } public function count(): int { return absint( $this->query->found_posts ); } }