Skip to content

Commit

Permalink
feat(pagination): allowing custom query to generate pagination (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Mar 24, 2022
2 parents fa084e4 + e48fbaa commit 662da66
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/Pagi.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,30 @@ public function __construct()
*/
protected function prepare()
{
$this->query = collect(
Arr::get($GLOBALS, 'wp_query')->query_vars ?? []
)->filter();
$isGlobalQuery = false;

if (! isset($this->query)) {
$this->query = collect(
Arr::get($GLOBALS, 'wp_query')->query_vars ?? []
)->filter();

$isGlobalQuery = true;
}

if ($this->query->isEmpty()) {
return;
}

$this->query->put('post_type', get_post_type());
if ($isGlobalQuery) {
$this->query->put('post_type', get_post_type());

if (is_tax()) {
$this->query->put('tax_query', [[
'taxonomy' => $this->query->get('taxonomy'),
'terms' => $this->query->get('term'),
'field' => 'name',
]]);
if (is_tax()) {
$this->query->put('tax_query', [[
'taxonomy' => $this->query->get('taxonomy'),
'terms' => $this->query->get('term'),
'field' => 'name',
]]);
}
}

$this->perPage = $this->query->get('posts_per_page');
Expand Down Expand Up @@ -100,4 +108,17 @@ public function build()
$this->currentPage
);
}

/**
* Set the WordPress query.
*
* @param WP_Query
* @return void
*/
public function setQuery($query)
{
$this->query = collect(
$query->query_vars ?? []
)->filter();
}
}

0 comments on commit 662da66

Please sign in to comment.