Describe the bug
Bug: WP_Query::max_num_pages becomes negative when using ep_integrate
Description
While integrating ElasticPress, I noticed an unexpected value for WP_Query::$max_num_pages.
With ep_integrate => true, the query returns:
found_posts = 17
max_num_pages = -17
The same query executed without ElasticPress integration does not exhibit this behavior.
A negative value for max_num_pages should never be possible, especially when found_posts is positive.
Query
$query = new WP_Query( [
// ...
'ep_integrate' => true,
] );
Observed values
var_dump( [
'found_posts' => $query->found_posts,
'max_num_pages' => $query->max_num_pages,
'posts_per_page' => $query->get( 'posts_per_page' ),
] );
Output:
found_posts => 17
max_num_pages => -17
posts_per_page => -1
Expected behavior
I would expect either:
max_num_pages = 17 if posts_per_page is treated as 1, or
max_num_pages = 0 or 1 when posts_per_page = -1 (no pagination),
but never a negative value.
Additional notes
From debugging, it appears the issue only occurs after ElasticPress populates the WP_Query object with the Elasticsearch response. The initial WP_Query instance does not contain the negative value.
Looking at the code, it seems max_num_pages is calculated directly from found_posts and posts_per_page. Since posts_per_page = -1 is a valid WordPress value meaning "return all posts", I believe this edge case should be handled explicitly to prevent max_num_pages from becoming negative.
I'm not yet sure whether this is caused by ElasticPress itself or by an interaction with WordPress, but I thought it was worth reporting since max_num_pages should never be negative.
Steps to Reproduce
## Steps to reproduce
1. Ensure ElasticPress is active and the index is up to date.
2. Create at least one published post that can be returned by a search query.
3. Run the following query:
```php
$query = new WP_Query( [
'post_type' => 'post',
's' => '',
'posts_per_page' => -1,
'ep_integrate' => true,
] );
```
4. Inspect the query object:
```php
var_dump( [
'found_posts' => $query->found_posts,
'max_num_pages' => $query->max_num_pages,
'posts_per_page' => $query->get( 'posts_per_page' ),
] );
```
5. If the search returns at least one result (for example, `found_posts = 17`), `max_num_pages` is set to `-17`.
## Expected behavior
When `posts_per_page = -1`, `max_num_pages` should not be negative. It should behave consistently with WordPress core, where pagination is effectively disabled.
Screenshots, screen recording, code snippet
No response
Environment information
WordPress and ElasticPress information
- WordPress: 7.0
- ElasticPress: 5.3.3
Code of Conduct
Describe the bug
Bug:
WP_Query::max_num_pagesbecomes negative when usingep_integrateDescription
While integrating ElasticPress, I noticed an unexpected value for
WP_Query::$max_num_pages.With
ep_integrate => true, the query returns:found_posts = 17max_num_pages = -17The same query executed without ElasticPress integration does not exhibit this behavior.
A negative value for
max_num_pagesshould never be possible, especially whenfound_postsis positive.Query
Observed values
Output:
Expected behavior
I would expect either:
max_num_pages = 17ifposts_per_pageis treated as1, ormax_num_pages = 0or1whenposts_per_page = -1(no pagination),but never a negative value.
Additional notes
From debugging, it appears the issue only occurs after ElasticPress populates the
WP_Queryobject with the Elasticsearch response. The initialWP_Queryinstance does not contain the negative value.Looking at the code, it seems
max_num_pagesis calculated directly fromfound_postsandposts_per_page. Sinceposts_per_page = -1is a valid WordPress value meaning "return all posts", I believe this edge case should be handled explicitly to preventmax_num_pagesfrom becoming negative.I'm not yet sure whether this is caused by ElasticPress itself or by an interaction with WordPress, but I thought it was worth reporting since
max_num_pagesshould never be negative.Steps to Reproduce
Screenshots, screen recording, code snippet
No response
Environment information
WordPress and ElasticPress information
Code of Conduct