I've got a remote source of data - customers and the items they've purchased - which resides on a different server and uses a system that's not Wordpress.

But it's got a REST API I can call to obtain this information, and what I'd like to do - via a plugin - is create a custom post type that wraps this external information.

So that, in the admin pages, you'd have a "Customers" custom post type and, when you clicked on it, the list would show the customers (as obtained via an external API call) then you could edit the information (which would make another API call to update the customer's data) and so on.

So that, to the user, it looks like they're interacting with a Wordpress custom post type in the usual way - but, actually, the data is not found in the "wp_posts" table at all. It exists on another system.

My first thoughts were to create the custom post type but then hook "the_posts" and manually create "fake posts". So that when any query queries this custom post type, I'd intercept that query and pass back a posts or posts that was dynamically created on-the-fly (using the query parameters to quiz the remote API and get back the data to make these "fake" posts reflect the external data).

I found a tutorial thing that suggested creating posts and then sticking it into the Wordpress cache with "wp_cache_add", as then, whenever Wordpress is looking for that (fake) post ID, it'll pull it out of the cache.

Which would be exactly right, if I could only get it to work.

I mean, I could create my own admin pages to list the "posts" and make my own edit page - mimicking Wordpress's interface but actually passing it via this API, rather than the "wp_posts" table - but that's needless duplication and would go out of date, as any changes to the Wordpress interface are made in future.

Wordpress goes to its database to find posts. But I want to intercept this, as the data source is not the local database, but a remote API call.

So I want to intercept queries - including those on the backend, as well as the frontend - then, if it relates to my custom post type, insert "fake" posts for that custom post type, which the rest of Wordpress treats as if it were a post in "wp_posts", but it's actually coming from elsewhere.

Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.