Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: add WP_REST_Posts_Controller route to fetch page counts #67719

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Dec 9, 2024

What?

Alternative to #66294

Extends WP_REST_Posts_Controller with a new route to fetch page counts

This PR only affects the page post type.

In the Core sync, all post types that inherit from WP_REST_Posts_Controller will get the /count route.

Why?

The need for a performant post counts check for the block editor. See: #65223 (comment)

Contrast this with $query->found_posts which returns the total count for a specific query only and would degrade performance given n posts.

How?

Via wp/v2/pages/count, the editor can fetch the total number of post counts.

This is done using wp_count_posts.

Testing Instructions

Run tests:

npm run test:unit:php:base -- --filter Gutenberg_Test_REST_Posts_Controller

Examples to run in the browser console (in the editor):

await wp.apiFetch( { path: '/wp/v2/pages/count' } )

/*
{
    "publish": 1,
    "future": 0,
    "draft": 2,
    "pending": 0,
    "private": 0,
    "trash": 0,
    "auto-draft": 0
}
*/

Also add a custom post status and ensure it appears in the response:

/**
 * Create initial post types.
 */
function create_initial_post_status_test() {
	register_post_status( 'ridgy_didge', array( 'public' => true ) );
}

add_action( 'init', 'create_initial_post_status_test', 0 ); // Highest priority
await wp.apiFetch( { path: '/wp/v2/pages/count' } )

/*
{
    "publish": 1,
    "future": 0,
    "draft": 2,
    "pending": 0,
    "private": 0,
    "trash": 0,
    "ridgy_didge": 0,
    "auto-draft": 0
}
*/

@ramonjd ramonjd added [Type] Enhancement A suggestion for improvement. REST API Interaction Related to REST API labels Dec 9, 2024
@ramonjd ramonjd self-assigned this Dec 9, 2024
@ramonjd ramonjd requested a review from spacedmonkey as a code owner December 9, 2024 05:30
Copy link

github-actions bot commented Dec 9, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ramonjd <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Extend post type controller to return total post count

Alternative to #66294
Extend post type controller to return total post count
@ramonjd ramonjd force-pushed the add/rest-api-extend-posts-controller-get-counts branch from 9505fde to 54bfc80 Compare December 9, 2024 06:14
@ramonjd ramonjd changed the title REST API: new endpoint to fetch post counts by post status REST API: add WP_REST_Posts_Controller route to fetch post counts by post status Dec 9, 2024
@ramonjd ramonjd changed the title REST API: add WP_REST_Posts_Controller route to fetch post counts by post status REST API: add WP_REST_Posts_Controller route to fetch page counts Dec 9, 2024
ramonjd added a commit to ramonjd/wordpress-develop that referenced this pull request Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REST API Interaction Related to REST API [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant