Skip to content

Instantly share code, notes, and snippets.

@nathanaelphilip
Created November 3, 2016 20:35
Show Gist options
  • Save nathanaelphilip/9ac597f693e076d38cc92f8bff5a7377 to your computer and use it in GitHub Desktop.
Save nathanaelphilip/9ac597f693e076d38cc92f8bff5a7377 to your computer and use it in GitHub Desktop.
CRON syncing
function saj_sync_jobs($locations)
{
// get all business locations
foreach ((array)$locations as $key => $location) {
saj_persist_jobs($location);
}
return $locations;
}
function saj_persist_job($value){
$title = $value->Title. ' ('. $value->Id .') ('. $value->JobId .')';
$options = [
'post_type' => 'snag-a-job',
'post_title' => $title,
'post_status' => 'publish',
];
$qry = new wp_query([
'post_type' => 'snag-a-job',
'nopaging' => true,
'no_found_rows' => true,
'meta_query' => [
[
'key' => 'Id',
'value' => $value->Id
]
],
'fields' => 'ids'
]);
if ($qry->have_posts()) {
$options['ID'] = $qry->posts[0];
}
$pid = wp_insert_post($options);
saj_persist_job_meta($pid,$value);
# update alias
$value->Terms = wp_set_post_terms($pid, [$value->Alias->term_id], $value->Alias->taxonomy);
$value->Persisted = true;
$value->PID = $pid;
return $value;
}
function saj_persist_job_meta($post_id,$value)
{
update_post_meta($post_id, 'Title', $value->Title);
update_post_meta($post_id, 'Requirements', $value->Requirements);
update_post_meta($post_id, 'AdditionalInformation', $value->AdditionalInformation);
update_post_meta($post_id, 'ApplyUrl', $value->ApplyUrl);
update_post_meta($post_id, 'LocationName', $value->LocationName);
update_post_meta($post_id, 'LocationAddress1', $value->LocationAddress1);
update_post_meta($post_id, 'LocationCity', $value->LocationCity);
update_post_meta($post_id, 'LocationState', $value->LocationState);
update_post_meta($post_id, 'Description', $value->Description);
update_post_meta($post_id, 'JobId', $value->JobId);
update_post_meta($post_id, 'Id', $value->Id);
update_post_meta($post_id, 'LocationId', $value->LocationId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment