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

BUG: Broken Search Pagination on Hierarchical Post Types #2480

Closed
tfrommen opened this issue Dec 1, 2021 · 2 comments · Fixed by #2511
Closed

BUG: Broken Search Pagination on Hierarchical Post Types #2480

tfrommen opened this issue Dec 1, 2021 · 2 comments · Fixed by #2511
Milestone

Comments

@tfrommen
Copy link
Contributor

tfrommen commented Dec 1, 2021

Describe the bug
When searching a hierarchical post type in the admin, for example, Pages (/wp-admin/edit-php?post_type=page), the pagination is broken. This is becacuse of a combined bug in WordPress in ElasticPress.

Steps to Reproduce

  1. Go to Pages in the admin: /wp-admin/edit-php?post_type=page
  2. Search for a word or phrase where you have lots of posts that include this.
  3. Use pagination buttons to navigate to, say, page 3.
  4. This will result in a request error for Elasticsearch.

Expected behavior
Pagination when searching hierarchical post types should just work.

Screenshots

Environment information
(This is irrelevant because the two places in WordPress and ElasticPress code have been introduced years and years ago, see further down.)

Additional context
The reason for this issue is the from argument in the query, which is -2 (for page 3).

This is because ElasticPress uses posts_per_page to calculate the offset, POSTS_PER_PAGE * ( PAGE - 1 ). For hierarchical post types, however, this value is overwritten to -1 by WordPress (most probably to fetch all posts so that posts to be shown on the current page that have a parent can be listed with their parent post).

Now, with posts_per_page being -1, this will lead to -1 * ( PAGE - 1 ), which is the following:

  • Page 2: -1
  • Page 3: -2
  • Page 4: -3
  • ...

Negative from values will lead to a null pointer exception, and thus a request error.

I think a fix for this would be to ensure that from is greater or equal to 0. That means, if posts_per_page is -1, just set from to 0, and it should all work. The actual pagination (i.e., showing the correct posts from all the posts fetched) will then be done by WordPress anyway.

@tfrommen tfrommen added the bug Something isn't working label Dec 1, 2021
@felipeelia
Copy link
Member

Hi @tfrommen, thanks for opening this issue! I'm able to reproduce the problem and your suggested fix seems to work fine. Do you have the availability to create a PR with that?

@felipeelia felipeelia added confirmed bug and removed bug Something isn't working labels Dec 9, 2021
@tfrommen
Copy link
Contributor Author

tfrommen commented Dec 9, 2021

@felipeelia done that over in #2511.

@felipeelia felipeelia added this to the 3.6.6 milestone Dec 14, 2021
@felipeelia felipeelia linked a pull request Dec 14, 2021 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants