We can get the total number of posts from the X-WP-Total
response header from the /wp-json/wp/v2/posts/
endpoint, e.g. using fetch()
in Javascript or apiFetch
from @wordpress/api-fetch
module in a custom block.
Since number of fetched posts doesn’t affect the X-WP-Total
header, we can use posts_per_page
as 1
and only get the post id field with _fields
, to reduce the payload.
We might also create a dynamic block, including this kind of PHP:
$total = (new \WP_Query( array( 'posts_per_page' => 1 ) ) )->found_posts;
for the total posts part.
An older way was to use e.g. a custom shortcode [remaining]
that outputs the
number of remaining posts:
add_shortcode( 'remaining', function( array|string $atts ) :string {
$milestone = 1000;
$total = (new \WP_Query( array( 'posts_per_page' => 1 ) ) )->found_posts;
return sprintf( '%s', $total > $milestone? 0 : $milestone - $total );
} );