Skip to content

Commit

Permalink
update next/previous ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Davies committed Feb 23, 2017
1 parent 38b592e commit fef7d73
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down

0 comments on commit fef7d73

Please sign in to comment.