query = new WP_Query([ 'post_type' => 'post', 'post_status' => 'publish', 'paged' => $this->page, 'posts_per_page' => $this->amount, 's' => $this->search, ]); $this->totalPages = $this->query->max_num_pages ?? 1; $this->totalPosts = $this->query->found_posts ?? 0; $this->page = min(max(1, $this->page), $this->totalPages); $start = ($this->page - 1) * $this->amount + 1; $end = min($this->totalPosts, $this->page * $this->amount); $this->range = "{$start}-{$end}"; $this->posts = $this->query ? collect($this->query->posts) ->map(fn ($post) => [ 'title' => get_the_title($post), 'url' => get_permalink($post), ])->all() : []; return view('livewire.post-search', [ 'hasPrevious' => $this->page > 1, 'hasNext' => $this->page < $this->totalPages, ]); } /** * Go to the next page. */ public function nextPage() { $this->page = min($this->totalPages, $this->page + 1); } /** * Go to the previous page. */ public function previousPage() { $this->page = max(1, $this->page - 1); } }