Pinball Failure

WP JSON API Challenges

In my frustration of my static process, I ripped everything out by it’s roots and decided to take things in a strict order.

Decision: All-In-One or Separate?

The first question was do I want to download everything as one big JSON file and split it out later, or do I want to, from the start, download each post as it’s own file? The time to download shouldn’t change, and if I do it right, I could script it to check if the date stamp on my downloaded file was older than the post date in WP. That means I could have this logic:

if ( ! post file exists || post file date < post datestamp ) {
    download JSON for this post

That made it an easy win. Let’s split away from the original plan. Now my goal is to extract all the posts from my WP site in JSON format.

Remember the goal here is to run a static website with WP. There are plugins that convert WP to static HTML, which you can then use to power a site, and this isn’t a bad idea.

How To: Get all the posts from a WP site via the JSON API?

This sounded easy. listed posts, and the v1 comparison chart says that you can list all posts, it stands to reason that I can easily get all my posts.

In a way, this is a WordPress problem. WP doesn’t want you to show all posts. It would make your site very slow if you had, say, 10,000 posts. Which means that the claim that the JSON API can lists all posts is true, it just doesn’t do them all at once.

I happen to have 40 published posts on my test site, so when I GET with this URL, it loads all of them:

But that isn’t sustainable. And the old trick of making per_page equal to -1 didn’t work.

I did determine that the JSON API actually knows what the amount of posts is!

JSON reports X-WP-Total which, in my case is 40

The header “X-WP-Total: 40” is what I needed. Of course, that is rather weird to get. It means to get all the files, I have to write ‘something’ that does this:

  1. Get and parse it to get the value of X-WP-Total
  2. Get

Okay. So how?

Well remember how I mentioned a plugin that made a static site already? I was talking about things like Really Static. Save a post, it saves the JSON file! Why not write this plugin?

Why not use “Really Static”? Well I don’t want to have WP doing more than be my content generator. I want to separate content (the blog) from WP. And that means I will end up writing a plugin…

Am I In Too Deep? Or Am I Over My Head?

So far, this has been an incredible amount of work. It’s been a lot of two steps forward and three steps back, over and over and over. And yes, it’s been very frustrating. I’ve given up many times and, if my posts about multiple approaches are any indication, I’ve deleted and restarted many, many times.

And I have to be honest here… I’m giving up on this overly ambitious plan right now.

This marks two major failures (three if I could how insane the entire ‘use Jekyll/Hugo to run a gallery’ was). First, I’ve failed to sensibly convert JSON files to MD in a scriptable and reproducible way. Second, I’ve failed to export JSON from WordPress in a simple way. Both those failures can be worked around. I can build a plugin that exports the JSON. It’s not trivial, but having done similar things I’m sure I can do it. The issue is ‘is it worth it’? And right now I think the answer is no.

I’m back to the drawing board right now, sketching up new plans and ideas. I want to eliminate as many steps as possible. I’d like to leave WordPress alone, let it be WordPress, and similarly let my static generator be static. These failures have taught me more about the interdependencies than I expected, and I’ve learned more from them than I may have if I’d had outright success from the start.

While I’m starting over, again, I feel hopeful and less ‘I hate you, JSON’ than I thought I would. I now understand things more clearly and I see how I made things far, far more complicated for myself.

The future of this project is, weirdly, bright.





%d bloggers like this: