Opened 4 weeks ago
Last modified 20 hours ago
#62390 new enhancement
REST API: add WP_REST_Posts_Controller route to fetch page counts
Reported by: | ramonopoly | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | trunk |
Component: | REST API | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description (last modified by )
The PR proposes to extend WP_REST_Posts_Controller
with a new route /count to fetch page counts, e.g., wp/v2/pages/count.
The total number of post counts is fetched by using wp_count_posts.
https://github.com/WordPress/gutenberg/pull/67719
There is a need for a performant post counts check in the block editor. See: https://github.com/WordPress/gutenberg/pull/65223#issuecomment-2381323815
Contrast this feature with $query->found_posts
which returns the total count for a specific query only and would degrade performance given n
posts.
Change History (4)
This ticket was mentioned in PR #7773 on WordPress/wordpress-develop by @ramonopoly.
4 weeks ago
#1
#2
@
39 hours ago
- Description modified (diff)
- Summary changed from REST API: proposal for new endpoint to fetch post counts by post status to REST API: add WP_REST_Posts_Controller route to fetch page counts
- Version changed from 6.7 to trunk
@ramonopoly commented on PR #7773:
24 hours ago
#3
I appreciate the quick review and suggestions.
This new endpoint needs tests.
I am aware. had already written a prominent TODO in the description to add them.
@ramonopoly commented on PR #7773:
20 hours ago
#4
Thanks for helping me think this over @spacedmonkey
Given everything I've tested I have a mind to revert back to https://github.com/WordPress/wordpress-develop/blob/95db722a2f73fd238312ed026f442c88363b3cd6/src/wp-includes/rest-api/endpoints/class-wp-rest-post-counts-controller.php, which introduced an embeddable, separate endpoint with a dedicated schema.
So far the only arguments to extend WP_REST_Posts_Controller
has been because it seems like the natural thing to do.
Is it worth trying add a custom schema for the /counts
route?
## What?
Syncs https://github.com/WordPress/gutenberg/pull/66294
The PR proposes a new endpoint to fetch the total number of post counts by post status using wp_count_posts.
## Why?
The need for a performant post counts check for the block editor. See: https://github.com/WordPress/gutenberg/pull/65223#issuecomment-2381323815
Contrast this with
$query->found_posts
which returns the total count for a specific query only and would degrade performance givenn
posts.## How?
Following the simplicity of wp/v2/statuses, which wraps get_post_stati, creating a basic route to return post type counts, e.g.,
wp/v2/counts/<post_type>
## Testing Instructions
Run the tests:
npm run test:php -- --filter=WP_Test_REST_Post_Counts_Controller
Open up the site editor and have a play with the console:
Examples:
Also add a custom post type and ensure it appears in the response:
function add_my_custom_post_type() { register_post_status( 'sweaty-palms', array( 'label' => _x( 'Sweaty palms', 'post' ), 'label_count' => _n_noop( 'Sweaty palms <span class="count">(%s)</span>', 'Sweaty palms <span class="count">(%s)</span>' ), 'public' => true, 'exclude_from_search' => false, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, ) ); } add_action( 'init', add_my_custom_post_type );
Trac ticket: https://core.trac.wordpress.org/ticket/62390