Make WordPress Core

Opened 8 weeks ago

Closed 4 weeks ago

Last modified 3 weeks ago

#66098 closed enhancement (fixed)

Add pre_wp_count_posts filter for the post counts query

Reported by: jdeep Owned by: abhishekfdd
Priority: normal Milestone: 7.2
Component: Posts, Post Types Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: performance

Description (last modified by westonruter)

wp_count_posts() offers no way to bypass its GROUP BY post_status aggregate and serve the counts from another source.

The result of wp_count_posts() is cached in the object cache under the key returned by _count_posts_cache_key( $type ). That key is deleted on every status transition. On a large site with frequent publishing the cache is therefore almost never warm: nearly every admin request misses and re-runs the full aggregate over the entire post type, which takes several seconds on a table with 1M+ posts.

For a large publisher, an exact real-time count in the admin dashboard is not important as long as they are able to publish without the list screen taking several seconds to load. We can happily trade a little count accuracy for performance.

We can run the aggregate in the background using Action Scheduler and store the result in a durable store (an option). But to make wp_count_posts() actually return that value and skip its query, we have to fill core's object cache by hand. On every request, we wp_cache_set() the stored counts under the key returned by internal _count_posts_cache_key() function, so core's wp_count_posts() hits the cache.

With a pre_wp_count_posts filter, wp_count_posts() could serve the pre-computed value (for whoever wants to trade off accuracy for performance) and skip the main query entirely.

Change History (12)

#1 @jdeep
8 weeks ago

This was initially proposed in #61097 for a totally different use-case (of overriding a less performant query) and the filter was later removed from the final patch as the query itself was updated.

Version 0, edited 8 weeks ago by jdeep (next)

#2 @jdeep
8 weeks ago

  • Description modified (diff)

#3 @westonruter
8 weeks ago

  • Description modified (diff)

#4 @westonruter
8 weeks ago

  • Keywords needs-patch added
  • Milestone Awaiting Review → 7.2

This seems to make sense to me.

@jdeep Do you want to open a PR?

#5 @jdeep
8 weeks ago

Sure @westonruter, will raise a PR for it.

I was waiting to get a second opinion on whether this is a worthwhile change.

#6 @westonruter
8 weeks ago

  • Owner set to jdeep
  • Status new → assigned

#7 follow-up: @abhishekfdd
6 weeks ago

@jdeep Happy to help if you haven't started, I have a patch ready.

#8 in reply to: ↑ 7 @jdeep
6 weeks ago

Replying to abhishekfdd:

@jdeep Happy to help if you haven't started, I have a patch ready.

Sure @abhishekfdd please go ahead!

#9 @jdeep
6 weeks ago

  • Owner changed from jdeep to abhishekfdd

This ticket was mentioned in ​PR #13846 on ​WordPress/wordpress-develop by ​@abhishekfdd.


6 weeks ago
#10

  • Keywords has-patch has-unit-tests added; needs-patch removed

Adds a pre_wp_count_posts filter to wp_count_posts(). Returning a non-null value short-circuits the function, skipping the object cache lookup and the GROUP BY post_status query, so sites with large post tables can serve counts from a precomputed source.

  • Runs after post_type_exists() and before the cache lookup.
  • The short-circuited value is not cached.
  • The value is still passed through wp_count_posts, and any registered status missing from it is set to 0.

Unit tests are in tests/phpunit/tests/post/wpCountPosts.php.

See #66098.

Trac ticket: [](https://core.trac.wordpress.org/ticket/66098)

Use of AI Tools

Tool(s): Claude Code
Model(s): Opus 5.5
Used for: Drafted the patch and unit tests. I reviewed the implementation, checked the design against existing patterns such as pre_count_users, and ran the new tests and the full post test group locally."

#11 @peterwilsoncc
4 weeks ago

  • Resolution → fixed
  • Status assigned → closed

In 64245:

Posts, Post types: Introduce pre-flight filter to wp_count_posts().

Introduces the pre-flight filter pre_wp_count_posts to wp_count_posts().

Returning a non-null value short-circuits wp_count_posts(), skipping both the object cache lookup and the database query. This allows the counts to be served from another source, such as a value pre-computed in the background, when an exact real-time count is not required.

Any registered post status missing from the filtered value are added to the result and set to zero.

The returned value is not cached and passed through the wp_count_posts filter.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13846.

Props abhishekfdd, jdeep, soean, westonruter.
Fixes #66098.

#12 @westonruter
3 weeks ago

In 64589:

Posts, Post Types: Add pre_wp_count_attachments filter.

Introduces the pre_wp_count_attachments filter to wp_count_attachments(), the counterpart of pre_wp_count_posts. Returning a stdClass object short-circuits the function, skipping the object cache lookup and both of its queries. Since the counts cache group is non-persistent, those queries otherwise run on every request that needs the counts, and on a site with millions of attachments the GROUP BY post_mime_type aggregate alone can take several seconds. The filter allows the counts to be served instead from a value pre-computed in the background.

Unlike post statuses, the set of MIME types is not known ahead of time, so only a missing trash count is filled in with zero. The filtered value should therefore contain a count for every MIME type matching $mime_type, and only those types. As with pre_wp_count_posts, the value is not cached and is passed through the wp_count_attachments filter.

The pre_wp_count_posts filter introduced in r64245 is brought in line with the new one. A filtered value now short-circuits only when it is a stdClass object: any other non-null value was previously cast to an object, turning false into a nonsensical result, and filling in missing counts on another class can trigger a dynamic property deprecation. Both filters also clone the returned object, so that a callback which keeps it, such as in a static variable, does not have it modified by core or by later callbacks on the wp_count_posts and wp_count_attachments filters.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/14210.
Follow-up to r64245.

Props jdeep, westonruter, peterwilsoncc.
See #66098.
Fixes #66291.

Note: See TracTickets for help on using tickets.