As often, what begins as a simple question or small need spins out a series of digging, experimenting, and ultimately spending more time remixing a featured image (by hand) and writing a blog post that 4 people may read.

Ahhh, 2026. That’s another grumbly post.

But here it is, I was rather taken by this fantastic web site created at Thompsons Rivers University called Indigenous Peoples in Education: A Stock Photo Collection really worth leaving here and going to appreciate what and why this site was created. Someone may have been inspired to nominate it for an Open Education Award for Excellence. Maybe.

Web site for photos of Indigenous people in Education, a grid of the first six photos each with title describing and naming the people represented, all linked to more detail. On the left the typical web navigation.
https://indigenouseducationstockphotos.trubox.ca/

At first I was warmed that this might have been built with the TRU Collector SPLOT, but is not. It makes good use of the same parent theme that I love, Fukasawa by Anders Noren. No big deal.

I was curious to know how many photos were in this collection (not to brag, but its built into the sidebar of TRU Collector, see example.)

Thus the wonder, Surely (and yes I am calling you Shirley) there is a simple web tool or method to get the total number of blog posts on any given WordPress site. I would think this is quite done with the WordPress Rest API.

In fact this was bubbling around my mind when I wrote about the BC Corrections Leadership site I built more than a few years ago for JIBC that was still running. Maybe at my near peak level of custom code work with the WordPress API I made a mobile version of the course content called the CorrLeader Navigator (blogged to ridiculous detail).

The problem with my first web searches is that no matter how much I jiggled by search terms, the bulk of the replies were efforts to get all the posts via the API, I just wanted to count them.

What I did get that every request to the API returned in the response header a total number count for posts. Google’s AI review eagerly offered code that does not work:

fetch('https://your-site.com')
  .then(response => {
    // Retrieve the custom WordPress header
    const totalPosts = response.headers.get('X-WP-Total');
    console.log('Total Published Posts:', totalPosts);
  });

I can tell without even trying because the URL you need to pass to fetch is not the web address but an API call.

Stupid Gemeni.

But the leading clue I found in StackOverflow is you can just use a WordPress API url that just fetches a data on a single post (the latest, FWIW) which for this humble blog would be http://cogdogblog.com/wp-json/wp/v2/posts?per_page=1

Snd indeed, in any browser, that returns a wad of JSON content about a single post. But you do not see the response headers. I found via the W3 reference on HTTP Response Headers a link to Rex Swain’s HTTP Viewer (See exactly what an HTTP request returns to your browser). This is the kind of little web tool I can see if one person’s effort to make something others can use, thank you Rex.

I fill out the form with the REST Api Url above

Entering http://cogdogblog.com/wp-json/wp/v2/posts?per_page=1 in Rex Swain’s HTTP response viewer

And boom I get results (and smiling at the URL of results noting its a perl script), in there noting X-Wp-Total: 5919 which is indeed my post count.

HTTP Response Header for http://cogdogblog.com/wp-json/wp/v2/posts?per_page=1

Now of course, this is a rather convoluted way to get this number, but in doing so, I have more of an understanding how the data is found and returned.

I thought about dusting off my HTML/Javascript chops and building a little tool anyone else could use, wouldn’t it make sense to have that kind of tool out there? it’s pretty simple, it could just take an input of the web URL, the API request easily assembled. If a fetch request returned an error, most likely it’s not a WordPress url. I bet Tom Woodward could build this in the time it takes me to make a sandwhich.

Or I could do what Everybody Else is Doing These Days and just ask Claude or one of his pals to build it for me. That’s not my jam, sorry.

For now, I cobbled some Javascript I can cut and paste into a browser console:

async function getTotalPostCount(siteUrl) {
    try {
        // Appending per_page=1 minimizes data transfer
        const response = await fetch(`${siteUrl}/wp-json/wp/v2/posts?per_page=1`);
        
        if (!response.ok) {
            throw new Error(`Probably not a WordPress site! status: ${response.status}`);
        }

        // Extract the special WordPress header
        const totalPosts = response.headers.get('X-WP-Total');
        
        console.log(`This WordPress site has, wow,  ${totalPosts} posts`);
        return parseInt(totalPosts, 10);
    } catch (error) {
        console.error('Error, likekly not a WordPress site?: ', error);
    }
}

// Example usage (Replace with your actual WordPress site URL)
getTotalPostCount('https://cogdogblog.com');

Boom, I get the 5919 blog post odometer at CogDogBlog.

I can try some other random b-blog 😉

Browser console session getting blog post count for Bavatuesdays, keep trying, kid!

But getting back to the original quest, via this (sort of) simple method I can find that the Indigenous Peoples in Education Stock Photo Collection has just under 200 images.

That’s a long way (and actually about 3 weeks later) to getting an answer. If we think of the web, and its various tools, machines as something to dispense an answer, well there, it maybe could be spat out of a vibe coded site in less time then it took me to remix my featured image (but that is fun, and I do not want machines doing that for me).

But what would I have learned about the underlying mechanisms? What could I extend to a different situation? Are we just seeking answers any more or are we after understanding?

I’ll take that over speed and efficiency any day of the week.


Featured Image: Before Computers, Adding Machines flickr photo by cogdogblog shared into the public domain using Creative Commons Public Domain Dedication (CC0) This was a machine spotted in the Willowbunch (SK) museum. My own photo edited where I plunked in a WordPress logo and redid the Burroughs logo in similar type and effect. Nobody generates images around here except me by hand.

If this kind of stuff has value, please support me by tossing a one time PayPal kibble or monthly on Patreon
Become a patron at Patreon!
Profile Picture for CogDog The Blog
An early 90s builder of web stuff and blogging Alan Levine barks at CogDogBlog.com on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person. And he is 100% into the Fediverse (or tells himself so) Tooting as @cogdog@cosocial.ca

Comments

  1. @barking
    My dad used to work in the maintenance filed of those types of calculators in the 1960s.
    I was really young but I played with that equipment or something very similar. Very cool memory for me.

Leave a Reply

Your email address will not be published. Required fields are marked *