From d0d090f448ed6e8c1dcc5ff527bca25fc2390d61 Mon Sep 17 00:00:00 2001 From: ArkaPrabhaChowdhury Date: Thu, 25 Jun 2026 13:07:33 +0530 Subject: [PATCH 1/2] Administration: Allow filtering the current user's post count --- .../includes/class-wp-posts-list-table.php | 16 +++++++++ .../phpunit/tests/admin/wpPostsListTable.php | 36 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php index 18c76169eb81c..ea92e8555db23 100644 --- a/src/wp-admin/includes/class-wp-posts-list-table.php +++ b/src/wp-admin/includes/class-wp-posts-list-table.php @@ -101,6 +101,22 @@ public function __construct( $args = array() ) { ) ); + /** + * Filters the number of posts authored by the current user. + * + * @since 7.1.0 + * + * @param int $user_posts_count Number of posts authored by the current user. + * @param string $post_type The post type. + * @param int $user_id The current user ID. + */ + $this->user_posts_count = (int) apply_filters( + 'user_posts_count', + $this->user_posts_count, + $post_type, + get_current_user_id() + ); + if ( $this->user_posts_count && ! current_user_can( $post_type_object->cap->edit_others_posts ) && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['all_posts'] ) diff --git a/tests/phpunit/tests/admin/wpPostsListTable.php b/tests/phpunit/tests/admin/wpPostsListTable.php index 9d2482a034af7..8988b2894791c 100644 --- a/tests/phpunit/tests/admin/wpPostsListTable.php +++ b/tests/phpunit/tests/admin/wpPostsListTable.php @@ -330,4 +330,40 @@ public function test_get_views_should_return_views_by_default() { $this->assertSame( $expected, $actual ); } + + /** + * @ticket 47640 + * + * @covers WP_Posts_List_Table::__construct + * @covers WP_Posts_List_Table::get_views + */ + public function test_get_views_should_use_filtered_user_posts_count() { + global $avail_post_stati; + + $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $user_id ); + + $filter = function ( $count, $post_type, $current_user_id ) use ( $user_id ) { + $this->assertSame( 0, $count ); + $this->assertSame( 'page', $post_type ); + $this->assertSame( $user_id, $current_user_id ); + + return 1; + }; + + add_filter( 'user_posts_count', $filter, 10, 3 ); + $table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-page' ) ); + remove_filter( 'user_posts_count', $filter ); + + $avail_post_stati_backup = $avail_post_stati; + $avail_post_stati = get_available_post_statuses(); + + $actual = $table->get_views(); + $avail_post_stati = $avail_post_stati_backup; + + $this->assertSame( + 'Mine (1)', + $actual['mine'] + ); + } } From c4298b6c80234b1a877f06caa20982f41e42e56f Mon Sep 17 00:00:00 2001 From: ArkaPrabhaChowdhury Date: Wed, 2 Sep 2026 18:30:22 +0530 Subject: [PATCH 2/2] Administration: add post-type user count filter --- .../includes/class-wp-posts-list-table.php | 25 ++++++++++++++++++- .../phpunit/tests/admin/wpPostsListTable.php | 13 ++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php index ea92e8555db23..39cd2b04c0a71 100644 --- a/src/wp-admin/includes/class-wp-posts-list-table.php +++ b/src/wp-admin/includes/class-wp-posts-list-table.php @@ -101,6 +101,8 @@ public function __construct( $args = array() ) { ) ); + $user_id = get_current_user_id(); + /** * Filters the number of posts authored by the current user. * @@ -114,7 +116,28 @@ public function __construct( $args = array() ) { 'user_posts_count', $this->user_posts_count, $post_type, - get_current_user_id() + $user_id + ); + + /** + * Filters the number of posts authored by the current user for a specific post type. + * + * The dynamic portion of the hook name, `$post_type`, refers to the post type. + * + * Possible hook names include: + * + * - `user_posts_post_count` + * - `user_posts_page_count` + * + * @since 7.1.0 + * + * @param int $user_posts_count Number of posts authored by the current user. + * @param int $user_id The current user ID. + */ + $this->user_posts_count = (int) apply_filters( + "user_posts_{$post_type}_count", + $this->user_posts_count, + $user_id ); if ( $this->user_posts_count diff --git a/tests/phpunit/tests/admin/wpPostsListTable.php b/tests/phpunit/tests/admin/wpPostsListTable.php index 8988b2894791c..c32a53d6442bc 100644 --- a/tests/phpunit/tests/admin/wpPostsListTable.php +++ b/tests/phpunit/tests/admin/wpPostsListTable.php @@ -343,18 +343,27 @@ public function test_get_views_should_use_filtered_user_posts_count() { $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $user_id ); - $filter = function ( $count, $post_type, $current_user_id ) use ( $user_id ) { + $filter = function ( $count, $post_type, $current_user_id ) use ( $user_id ) { $this->assertSame( 0, $count ); $this->assertSame( 'page', $post_type ); $this->assertSame( $user_id, $current_user_id ); return 1; }; + $post_type_filter = function ( $count, $current_user_id ) use ( $user_id ) { + $this->assertSame( 1, $count ); + $this->assertSame( $user_id, $current_user_id ); + + return 2; + }; add_filter( 'user_posts_count', $filter, 10, 3 ); + add_filter( 'user_posts_page_count', $post_type_filter, 10, 2 ); $table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-page' ) ); remove_filter( 'user_posts_count', $filter ); + remove_filter( 'user_posts_page_count', $post_type_filter ); + $avail_post_stati_backup = $avail_post_stati; $avail_post_stati = get_available_post_statuses(); @@ -362,7 +371,7 @@ public function test_get_views_should_use_filtered_user_posts_count() { $avail_post_stati = $avail_post_stati_backup; $this->assertSame( - 'Mine (1)', + 'Mine (2)', $actual['mine'] ); }