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

Ensure 'found_posts' is correct when no limit queries are in use #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

r-a-y
Copy link

@r-a-y r-a-y commented Sep 21, 2020

Currently, if a no limit query is set for WP_Query, the returned 'found_posts' property is zero instead of the number of posts.

'found_posts' is set to 'NA' here:

if ( empty( $limits ) || ( isset( $query->query_vars['no_found_rows'] ) && $query->query_vars['no_found_rows'] ) )
$this->found_posts = 'NA';

And never redeclared.

So when advanced-post-cache gets to returning the found posts count:

if ( $this->found_posts && is_array( $this->all_post_ids ) ) // is cached
return (int) $this->found_posts;

It returns 0 because of (int) 'NA'.

This PR fixes this by returning the count of the cached post IDs if 'found_posts' is a no limit query ('NA'). This is important for plugins doing checks against 'found_posts' and are anticipating a non-zero count. For example, bbPress does such a check here: https://github.com/bbpress/bbPress/blob/b772b4503991e3d55a34a4683a1f84e60decbce1/src/includes/replies/template.php#L212-L213

This addresses #4.

Currently, if 'no_found_rows' is set to false for WP_Query, the
returned 'found_rows' property is zero instead of the number of posts.

This commit fixes the 'found_posts' count by returning the number of
posts found in the cache.

Fixes Automattic#4.
@tomjn
Copy link

tomjn commented Sep 21, 2020

You're right, this value should be 0 not NA. 0 is what WP_Query uses

As for BBPress instead of:

	// Only add reply to if query returned results
	if ( ! empty( $bbp->reply_query->found_posts ) ) {

It should be:

	// Only add reply to if query returned results
	if ( $bbp->reply_query->have_posts() ) {

@r-a-y
Copy link
Author

r-a-y commented Sep 21, 2020

found_posts should not return 0 for no-limit queries. It should always return the proper post count. Check the value when this plugin isn't activated.

Switching to posts_pre_query (#10) and adding the proper 'found_posts' count property would also fix this if implemented correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants