0

I have CPT transcript in wordpress website. And create 250 post in CPT transcript.

Now, I want to fetch all transcript data and want to store in another database using rest api url http://example.com/wp-json/wp/v2/transcript/

AND

HTTP request using CURL

$ch = curl_init();
$url = 'http://example.com/wp-json/wp/v2/transcript/'; 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
return curl_exec($ch);
curl_close($ch);

But this return only 10 transcript,

Please help me, how to retrieve all transcript post data.

1
  • I believe this is what you're looking for.
    – Johansson
    Commented Sep 19, 2019 at 15:00

2 Answers 2

1

The REST API supports grabbing a maximum of 100 posts at a time, so you'll have to make an additional call for every 100.

Start with wp_remote_get() for 100 posts. While you're at it, go ahead and retrieve the body of this page and decode it:

$transcripts = wp_remote_get('http://example.com/wp-json/wp/v2/transcript/?per_page=100');
$transcriptsBody = wp_remote_retrieve_body($transcripts);
$transcriptsDecoded = json_decode($transcriptsBody, true);

Next, identify how many pages of 100 currently exist:

$numPages = wp_remote_retrieve_header($transcripts, 'x-wp-totalpages');

Then, if there's more than 1 page, grab each additional page. Start at page 2, since you've already grabbed page 1.

if($numPages > 1) {
    for($i=2; $i<($numPages+1); $i++) {
        // Identify the next endpoint to call
        $nextUrl = 'http://example.com/wp-json/wp/v2/transcript/?per_page=100&page=' . $i;
    }
    // Remote_get that endpoint
    $nextPage = wp_remote_get("$nextUrl");
    // Set a variable variable name for the next page
    $bodyVar = 'transcripts' . $i;
    // Get the body of the current page
    $$bodyVar = wp_remote_retrieve_body($nextPage);
    // Set a variable variable name for the decoded body
    $decodeVar = 'transcriptsDecoded' . $i;
    // Decode the JSON
    $$decodeVar = json_decode(${$bodyVar}, true);
    // Finally, merge the posts into the existing decoded array
    $transcriptsDecoded = array_merge($transcriptsDecoded, $$decodeVar);
}

You'll now have a PHP array of all of the posts, no matter how few or many posts there are.

Note that if you pull using the REST API and save the posts to a different database, you'll have a static copy. If anyone updates, deletes, or adds to the original site, your copy won't be in sync. So, it's more common to build the REST API calls into the code of the other site where you want to display the content, so that it reaches out and grabs the specific content you want to display right then and there. (And ideally, caches that for at least a few hours or days, so you're not constantly waiting on data to reload.)

1
  • Thanks, Its working fine
    – K. Pro
    Commented Sep 26, 2019 at 7:17
1

You can as suggested in one of the comments add &filter[posts_per_page]=250 but that is a pretty big response that may not be handled very well at either end.

Maybe a better approach as suggested under pagination is to add ?per_page=100 on the first request and then make a few additional requests with ?per_page=1&page=2 etc.

If you find that 100 at a time is still to high, try a lower number and request additional pages as needed.

If you need help with looping and/or concatenation, let me know and I can expand this.

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.