Commit 2b9f519786 for wordpress.org

commit 2b9f519786ff80997ed0bcf62ab01c8b87098566
Author: peterwilsoncc <peterwilsoncc@git.wordpress.org>
Date:   Thu Oct 8 22:45:50 2026 +0000

    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.


    Built from https://develop.svn.wordpress.org/trunk@64245


    git-svn-id: http://core.svn.wordpress.org/trunk@63396 1a063a9b-81f0-0310-95a4-ce76da25c4cd

diff --git a/wp-includes/post.php b/wp-includes/post.php
index a95288700b..8d9b58c037 100644
--- a/wp-includes/post.php
+++ b/wp-includes/post.php
@@ -3524,6 +3524,43 @@ function wp_count_posts( $type = 'post', $perm = '' ) {
 		return new stdClass();
 	}

+	/**
+	 * Filters the post counts before the query is run.
+	 *
+	 * 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
+	 * {@see 'wp_count_posts'} filter.
+	 *
+	 * @since 7.2.0
+	 *
+	 * @param stdClass|null $counts An object containing the post counts by status,
+	 *                              or null to run the default query. Default null.
+	 * @param string        $type   Post type.
+	 * @param string        $perm   The permission to determine if the posts are 'readable'
+	 *                              by the current user.
+	 */
+	$counts = apply_filters( 'pre_wp_count_posts', null, $type, $perm );
+
+	if ( null !== $counts ) {
+		$counts = (object) $counts;
+
+		foreach ( get_post_stati() as $status ) {
+			if ( ! isset( $counts->{$status} ) ) {
+				$counts->{$status} = 0;
+			}
+		}
+
+		/** This filter is documented in wp-includes/post.php */
+		return apply_filters( 'wp_count_posts', $counts, $type, $perm );
+	}
+
 	$cache_key = _count_posts_cache_key( $type, $perm );

 	$counts = wp_cache_get( $cache_key, 'counts' );
diff --git a/wp-includes/version.php b/wp-includes/version.php
index b65bf50db3..dca106f15d 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
  *
  * @global string $wp_version
  */
-$wp_version = '7.2-alpha-64244';
+$wp_version = '7.2-alpha-64245';

 /**
  * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.