Make WordPress Core

Changeset 64245


Ignore:
Timestamp:
10/08/2026 10:44:53 PM (5 weeks ago)
Author:
peterwilsoncc
Message:

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.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r64235 r64245  
    35253525        }
    35263526
     3527        /**
     3528         * Filters the post counts before the query is run.
     3529         *
     3530         * Returning a non-null value short-circuits wp_count_posts(), skipping both
     3531         * the object cache lookup and the database query. This allows the counts to
     3532         * be served from another source, such as a value pre-computed in the
     3533         * background, when an exact real-time count is not required.
     3534         *
     3535         * Any registered post status missing from the filtered value are added
     3536         * to the result and set to zero.
     3537         *
     3538         * The returned value is not cached and passed through the
     3539         * {@see 'wp_count_posts'} filter.
     3540         *
     3541         * @since 7.2.0
     3542         *
     3543         * @param stdClass|null $counts An object containing the post counts by status,
     3544         *                              or null to run the default query. Default null.
     3545         * @param string        $type   Post type.
     3546         * @param string        $perm   The permission to determine if the posts are 'readable'
     3547         *                              by the current user.
     3548         */
     3549        $counts = apply_filters( 'pre_wp_count_posts', null, $type, $perm );
     3550
     3551        if ( null !== $counts ) {
     3552                $counts = (object) $counts;
     3553
     3554                foreach ( get_post_stati() as $status ) {
     3555                        if ( ! isset( $counts->{$status} ) ) {
     3556                                $counts->{$status} = 0;
     3557                        }
     3558                }
     3559
     3560                /** This filter is documented in wp-includes/post.php */
     3561                return apply_filters( 'wp_count_posts', $counts, $type, $perm );
     3562        }
     3563
    35273564        $cache_key = _count_posts_cache_key( $type, $perm );
    35283565
Note: See TracChangeset for help on using the changeset viewer.