1

I have been trying different wordpress intermediate challenges, I came across one today that brought up a question I had no idea how to resolve it requires displaying data from a rest api however the actual display of the data needs to come from a post type.

So my question is: how can I fill post type, posts from a rest api?like the movie dB rest api or book rest api used for practical examples?

Sadly I can't seem to find much discussion in the way of this so I've resorted to all text based question documentation or tutorials would be appreciated I in now way expect anyone to type the exact process out to me this is simply me trying to gather information

2
  • I'm a bit confused as to what you are asking - do you want to retrieve WordPress post data from a WordPress REST API and then insert that data as new posts in to either the same or a separate installation as the REST API host? Or are you just trying to retrieve posts of a certain type from a WP REST API? Or... I'm also not sure what "filling a post type" means.
    – bosco
    Commented Aug 1, 2021 at 22:33
  • In essence I want to pull data from a Outside rest api Then create a new post for every instance of that data
    – Azurry
    Commented Aug 3, 2021 at 5:59

1 Answer 1

1

The short version is that you'd leverage WordPress's HTTP API to acquire your data from the external API (or perhaps that service's dedicated PHP library, if they provide one), then wp_insert_post() to insert that data into new posts. There is nothing unique about how you would register or handle an associated CPT to this end.

Assuming this was to be a sort of "synchronization routine" ran at regular intervals via WP_Cron or otherwise, you'd probably also want to store a timestamp for the last time data was synchronized, and only ask the remote API for content which is newer than that stamp (or more robustly, perhaps a few minutes before than that timestamp to account for potential edge-cases), using a piece of post meta to store the remote service's identifier for that piece of data in order to prevent processing data into duplicate posts.

If the amount of incoming data is substantial enough that it risks running out the clock on PHP script execution limits, you'd probably want to queue up requests for smaller chunks and work through them using a background queues - Woo's Action Scheduler is a pleasant way to accomplish this. It uses a technique of performing HTTP requests locally to the same installation, allowing it to execute whatever functionality within the WordPress environment independent of any front-end request, and cascade requests if necessary in order to handle long-running routines.

There's probably a lot more which could be said on the subject, but the specifics of the implementation are heavily dependent on the actual use-case. Keeping the data in all posts synchronized with that in some external API is a whole other ball game, for instance, and would be heavily dependent on the functionality which the API offers.

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.