Fix some issues with global queries #13
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi!
I was encountering a number of issues, so I thought I would open a pull request, if not to solve those issues right away, at least to pick your brain.
Multiple terms as query var
The first issue was that I couldn't paginate a post type archive page that uses a taxonomy to filter with multiple terms as query vars. Ex.: http://website.com/projects/?project-category[]=slug1&project-category[]=slug2. I ended up plainly removing the
post_type
addition andis_tax
check at Pagi.php:71 and all that was in that block, since it was recreating wrongly the current global query. An argument could be made thatis_tax
shouldn't be true on that page since it's a custom post type archive, but the fact is that WordPress does differently: when terms are in query vars, it activatesis_tax => true
onWP_Query
, hence the error. But, in the end, theis_tax
block shouldn't even be needed because of all the query vars that we pluck a few lines up and should already be defined.URLs with query strings getting re-encoded
Around LengthAwarePaginator.php:32, the functions that generate the current page, previous page and next page's URL are escaping by default and if there's a query string with multiple params, html characters get re-encoded. The only function down the line that allows us to not escape to avoid a double-escape situation is the
get_pagenum_link
function that I used to recreateget_next_posts_page_link
andget_previous_posts_page_link
.Unecessary round trip to the database
I was wondering if there was an explanation for the (get_posts)[https:/Log1x/pagi/blob/master/src/Pagi.php#L84] which makes an extra round trip to the database. In any case, I figured it was simply to create an
Illuminate\Support\Collection
to pass toLengthAwarePaginator
and let the Laravel Paginators do their magic, but if there's no use to have the posts data in there, the better solution to optimize this was to simply create an empty collection with therange
function and using the$query->found_posts
to fill it up to the right amount which, in turns, allows the creation of the pagination correctly.