Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pagination for custom WP_Query #10

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}