advanced-post-cache icon indicating copy to clipboard operation
advanced-post-cache copied to clipboard

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

Open r-a-y opened this issue 4 years ago • 2 comments

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: https://github.com/Automattic/advanced-post-cache/blob/c46b2d95773689938ef1e89a3dfcb9cbd94022b8/advanced-post-cache.php#L200-L201

And never redeclared.

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

https://github.com/Automattic/advanced-post-cache/blob/c46b2d95773689938ef1e89a3dfcb9cbd94022b8/advanced-post-cache.php#L230-L231

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.

r-a-y avatar Sep 21 '20 22:09 r-a-y

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() ) {

tomjn avatar Sep 21 '20 22:09 tomjn

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.

r-a-y avatar Sep 21 '20 22:09 r-a-y