From fef7d738b967a27c2870f4b86804b6b01cf2247a Mon Sep 17 00:00:00 2001 From: Karl Davies Date: Thu, 23 Feb 2017 14:00:48 +0000 Subject: [PATCH] update next/previous ordering --- src/Post.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Post.php b/src/Post.php index f728e37..6991d24 100644 --- a/src/Post.php +++ b/src/Post.php @@ -163,20 +163,23 @@ public function getContent() /** * Get the next post. * + * @param bool $scopeToCat Scope the return to the initial post's category * @return mixed */ - public function next() + public function next($scopeToCat = true) { if ( ! $this->next) { - $query = $this->where('ID', '>', $this->ID) + $query = $this->where('post_date', '>', $this->post_date) ->published() ->type('post'); - $query->whereHas('taxonomies', function ($query) { - $query->category()->whereHas('term', function ($q2) { - $q2->where('name', '=', $this->getMainCategoryAttribute()); + if ($scopeToCat) { + $query->whereHas('taxonomies', function ($query) { + $query->category()->whereHas('term', function ($q2) { + $q2->where('name', '=', $this->getMainCategoryAttribute()); + }); }); - }); + } $this->next = $query->orderBy('post_date', 'asc')->first(); } @@ -187,19 +190,22 @@ public function next() /** * Get the previous post. * + * @param bool $scopeToCat Scope the return to the initial post's category * @return mixed */ - public function previous() + public function previous($scopeToCat = true) { if ( ! $this->previous) { - $query = $this->where('ID', '<', $this->ID) + $query = $this->where('post_date', '<', $this->post_date) ->published() ->type('post'); - $query->whereHas('taxonomies', function ($query) { - $query->category()->whereHas('term', function ($q2) { - $q2->where('name', '=', $this->getMainCategoryAttribute()); + if ($scopeToCat) { + $query->whereHas('taxonomies', function ($query) { + $query->category()->whereHas('term', function ($q2) { + $q2->where('name', '=', $this->getMainCategoryAttribute()); + }); }); - }); + } $this->previous = $query->orderBy('post_date', 'desc')->first(); }