Custom endpoint and X-WP-TotalPages (headers)

I had a quick look and here’s how the headers are set in the WP_REST_Posts_Controller::get_items() method:

$response  = rest_ensure_response( $posts );
// ...
$response->header( 'X-WP-Total', (int) $total_posts );
$response->header( 'X-WP-TotalPages', (int) $max_pages );
// ...
return $response;

where:

$total_posts = $posts_query->found_posts;

and we could use as @jshwlkr suggested:

$max_pages = $posts_query->max_num_pages;

You could emulate that for your custom endpoint, by using WP_Query instead of get_posts().

Thanks to @DenisUyeda for correcting the typo.

Leave a Comment