Created
October 11, 2018 00:33
-
-
Save aliboy08/e577ed806e906d51237150e3a82b4286 to your computer and use it in GitHub Desktop.
PHP create posts from xml feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$feed_url = 'https://batchaws.adcourier.com/panel/?q=U2FsdGVkX19JpL80ZgsoSN4F3N3Ut1LGKwgX7VVH7q4RgdsLDocx-3L9VEoNT8WW'; | |
$ch = curl_init(); | |
curl_setopt ($ch, CURLOPT_URL, $feed_url); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
$xml = curl_exec($ch); | |
$jobs_feed = simplexml_load_string($xml); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$feed_url = 'https://batchaws.adcourier.com/panel/?q=U2FsdGVkX19JpL80ZgsoSN4F3N3Ut1LGKwgX7VVH7q4RgdsLDocx-3L9VEoNT8WW'; | |
$jobs_feed = simplexml_load_file($feed_url); | |
if( !$jobs_feed ) return; | |
$xml_feed_job_names = array(); | |
// Convert to array for reorder | |
$jobs_array = array(); | |
foreach( $jobs_feed as $job ) { | |
array_push($jobs_array, $job); | |
array_push($xml_feed_job_names, wp_strip_all_tags( (string)$job->job_title ) ); | |
} | |
// Check if job posts is in xml feed | |
// Remove existing posts if not in feed | |
$job_posts_query = get_posts(array( | |
'post_type' => 'job', | |
'showposts' => -1, | |
)); | |
if( $job_posts_query ) { | |
foreach( $job_posts_query as $job ) { | |
if( !in_array( $job->post_title, $xml_feed_job_names ) ) { | |
// Job post not in xml feed, delete post | |
wp_delete_post( $job->ID ); | |
} | |
} | |
} | |
$jobs_array_reversed = array_reverse($jobs_array); | |
// Scan from bottom to top, for correct date posted order | |
foreach( $jobs_array_reversed as $job ) { | |
$job_name = wp_strip_all_tags( (string)$job->job_title ); | |
$job_exists = get_page_by_title($job_name, OBJECT, 'job'); | |
if( !$job_exists ) { | |
// Create post | |
$post_args = array( | |
'post_type' => 'job', | |
'post_title' => $job_name, | |
'post_content' => (string)$job->job_description, | |
'post_status' => 'publish', | |
); | |
$job_date = false; | |
if( $job->posted_epoch ) { | |
$timestamp = (int)$job->posted_epoch; | |
$job_date = date('F d, Y', $timestamp); | |
$post_publish_format = date('Y-m-d H:i:s', $timestamp); | |
$post_args['post_date'] = $post_publish_format; | |
} | |
$post_id = wp_insert_post( $post_args ); | |
if( $post_id ){ | |
$update_metas = array(); | |
if( $job->unique_reference ) $update_metas['unique_reference'] = (string)$job->unique_reference; | |
if( $job->job_reference ) $update_metas['job_number'] = (string)$job->job_reference; | |
if( $job->job_summary ) $update_metas['summary'] = (string)$job->job_summary; | |
if( $job->application_url ) $update_metas['application_url'] = (string)$job->application_url; | |
if( $job->location_place ) $update_metas['location'] = (string)$job->location_place; | |
if( $job->job_type ) $update_metas['work_type'] = (string)$job->job_type; | |
if( $job_date ) { | |
$update_metas['date_posted'] = $job_date; | |
} | |
// Update post meta | |
foreach( $update_metas as $k => $v ) { | |
update_post_meta( $post_id, $k, $v ); | |
} | |
// Update post terms | |
if( $job->category ) { | |
ff_set_post_term( $post_id, (string)$job->category, 'job_cat' ); | |
} | |
if( $job->location_state ) { | |
ff_set_post_term( $post_id, (string)$job->location_state, 'state' ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment