Get all posts from WordPress REST API

As you may know, WP REST API has a limit of 100 records per call. This is what it says in the API Docs:

cap

Now this may be enough for some people, but what if you need to get ALL of them. Here are two solutions that I came up with.

1. Using PHP

We’re using PHP’s multi cURL. This means that we are doing multiple REST calls, but doing them simultaneously. Meaning, the amount of time to do the requests – is not dependent on the number of calls you make (because they’re asynchronous).

So let’s begin. First in your functions.php, create a function:

Inside your new function, is where we put the rest of our code.

Let’s figure out how many calls we need to make:

Let’s create an empty array and stuff it with the curl options:

As you can see above, we’re simply looping through the $callsToMake variable, to add a $page as part of our querystring. Similar to a paging system. As part of our loop, we are passing the options for curl through curl_setopt().

Now for the calls:

First we create a handle $mh – this is what curl uses to execute and initialize the class. We loop through our $callstoMake once more to add the handles to curl_multi_add_handle().

We do a do while, and execute curl. Now if if there is an error in any of the calls, we will get them in curl_getinfo().

Finally, we get all of the contents of our calls through curl_multi_getcontent(), and we stuff it in our newly created $responses array.

Now our $responses will look something like:

So we need to loop through them to create a big array of responses (in order).

So we do a final loop and do an array_push().

Then, we return our array as a JSON string by doing json_encode($final).

Now all we have to do is call our PHP function “getAllRecordsFromRest()” and we should have all of our posts.

2. Using JavaScript

We also have the option of using JavaScript for our multiple calls. Let’s start by getting the number of calls in our functions.php file:

Now, in our header.php – let’s create a JavaScript variable that stores the number of calls to make – from the function that we created above:

Now, let’s create an array, that houses all of the responses, from our AJAX calls.

We then to a “.when()” to fire our calls. And doing a console.log() on the arguments, we see the responses. This technique is described in my previous post Working with Promise and Deferred.

Using JS

Finally, let’s do a couple of for loops to merge the contents into one big array.

And that should do it. Using either of these techniques should overcome the 100 record limitation of pulling records from WordPress REST api.

 

Leave a Comment.