0

Some time ago there was a Wordpress plugin (Post Countdown) that created a widget to display how many posts there were still to be published before a milestone was reached. As of PHP8 it no longer works. Mind you it hasn't been updated for 10 years either.

A sample output was: "There are 600 posts until the 1000th post appears"

The means to do this was to get the current number of posts published on a blog and then subtract that number from the milestone post.

The plugin is more advanced than that, but the general idea seems simple enough.

Is there a simple code that can be inserted into an HTML or text widget to display this information?

Thanks for any help on this, it's appreciated from a newbie to coding.

1 Answer 1

0

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 );
} );
2
  • Thanks for the guidance, really appreciate it!
    – Mr. CRJ
    May 14, 2023 at 0:01
  • Glad to hear it helped
    – birgire
    May 14, 2023 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.