Loop through an iteration of 500 results at a time and increment page and restart with next 500
Question
Below, I will explain all the steps and how everything works so you get a better understanding of what I’m trying to achieve.
- The import_employees function kicks off and grabs the
$settings['page'] = 1'
which is the first page. - Then once the script finishes, it increments the current_page if the next_page is not null and restarts the script with the next batch of 500 until it reaches the last page and then resets the current_page to 1.
Does anyone know what I might be doing wrong? All the info provided will be helpeful.
var_dump($records)
reveals the following information:
object(stdClass)[1336] public 'paginator' => object(stdClass)[1335] public 'total' => int 3081 public 'per_page' => int 500 public 'current_page' => int 1 public 'last_page' => int 7 public 'prev_page' => null public 'next_page' => string 'https://oc.com/api/v1/employees/?page=2' (length=61)
var_dump($next_page);
reveals the following:
array (size=3) 'page' => int 2 'started' => int 1572849045 'finished' => int 0
Full code is listed here:
function import_employees()
{
$log = [];
$start_script = false;
// $name = 'octopus_import_employees';
$settings = get_settings();
$octopus = get_octopus();
/* Start the script at 0 */
$settings['started'] = 0;
$settings['page'];
/* If the started timestamp is less than or equal to finished, start the script */
if ($settings['started'] <= $settings['finished']) {
$start_script = true;
}
/* If start script is true, continue */
if ($start_script) {
/* Grab the first 500 records from Octopus */
$records = $octopus->get_raw('employees', [
'updated_at_after' => $settings['started'],
]);
/* Once the time is fixed, if no records need updating */
if (!$records) {
echo '<pre>No updates for offices in Octopus.</pre>';
return;
}
/* Returns all the Octopus records as an object */
foreach ($records->data as $record) {
$sync = false;
$time_changed = false;
// If employee with octopus id does exist
if (octopus_id_exists($record->id)) {
$wp_employee = get_id($record->id);
/* Grab the sync settings */
$sync_settings = get_post_meta($wp_employee->ID, '_setting_update', true);
/* Change WP last modified timestamp to Unix */
$wp_modified = strtotime(get_post_meta($wp_employee->ID,'_last_modified', true));
}
}
if (!is_null($records->paginator->next_page)) {
$next_page = $records->paginator->current_page++;
var_dump($next_page);
$settings['page'] = $next_page;
$settings['started'] = time();
$settings['finished'] = 0;
var_dump($settings);
} else {
$settings['page'] = 1;
$settings['finished'] = time();
}
}
}
function get_settings()
{
/* Set the default page, started and finished */
return [
'page' => 1,
'started' => 0,
'finished' => 0,
];
}
0
loop, posts, wp-query
4 years
2019-11-04T00:36:16-05:00
2019-11-04T00:36:16-05:00 0 Answers
72 views
0
Leave an answer