diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 097f8186e9..20c4d4da42 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -2647,6 +2647,13 @@ function wp_count_posts( $type = 'post', $perm = '' ) { $counts = wp_cache_get( $cache_key, 'counts' ); if ( false !== $counts ) { + // We may have cached this before every status was registered. + 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 ); } diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index ca18c41b0e..14cfb31d16 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -879,6 +879,22 @@ class Tests_Post extends WP_UnitTestCase { $this->assertNotEquals( $initial_counts->publish, $after_trash_counts->publish ); } + /** + * @ticket 49685 + */ + function test_wp_count_posts_status_changes_visible() { + self::factory()->post->create_many( 3 ); + + // Trigger a cache. + wp_count_posts(); + + register_post_status( 'test' ); + + $counts = wp_count_posts(); + $this->assertTrue( isset( $counts->test ) ); + $this->assertEquals( 0, $counts->test ); + } + /** * @ticket 13771 */