Created
January 11, 2013 07:26
-
-
Save ankitnetwork18/4508696 to your computer and use it in GitHub Desktop.
wordpress: import data into wordpress from rss/xml data
This file contains hidden or 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
/* | |
* This is wordpress API for importing latest news from cnn ibn | |
* http://localhost/Sandbox/wordpress/wp-content/plugins/data_import/import_news18_images.php | |
*/ | |
//load wordpress functions | |
require_once("../../../wp-load.php"); | |
include 'functions.php'; | |
/** | |
* xml should not be blank, and if it is generated blank data should not be inserted into db | |
* | |
*/ | |
// http://stg.ibnlive.com/intt/news18api/ | |
// url to get xml containing latest posts | |
// http://localhost/Sandbox/wordpress/wp-content/plugins/data_import/import_news18_images.php | |
/* | |
* data to be send in curl request | |
*/ | |
$post_data = array( | |
'data_type' => 'videos', | |
'action' => 'submit' | |
); | |
/* | |
* Post curl request to get latest image posts from cnn ibn | |
*/ | |
$data = post_curl('http://stg.ibnlive.com/intt/news18api/get_video_posts.php', $post_data); | |
$xml = simplexml_load_string($data); | |
$size = count($xml->category->node); | |
$hasData = false; | |
$inserted_post_ids = array(); | |
if($size>0) { | |
$hasData = true; | |
} | |
if($hasData) { | |
//start inserting records | |
foreach($xml->category->node as $node) { | |
//to be inserted through wp_insert_post | |
$post_title = (String)$node->headline; | |
$post_name = sanitize_title((String)$node->headline); | |
$post_content = (String)$node->intro; | |
$tags_input = (String)$node->keywords; | |
$post_date = get_wp_date((String)$node->creation_date); | |
$post_modified = (String)$node->last_update_date; | |
//to be inserted through post meta | |
$old_content_id = (int)$node->content_id; | |
$post_type = 'video'; | |
$post_agency = (String)$node->agency; | |
$post_byline = (String)$node->byline; | |
$video_url = (String)$node->video_url; | |
// check for post already exists or not | |
$postExists = false; | |
$sql = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'old_content_id' AND meta_value = $old_content_id"; | |
$result = $wpdb->get_col($sql); | |
$old_post_id = (int)$result[0]; | |
if($old_post_id>0){ | |
$postExists = true; | |
} | |
//if the post does not already exists in wordpress, insert it | |
if(!$postExists){ | |
//prepare the post array to be inserted in db | |
$this_post = array( | |
'post_title' => $post_title, | |
'post_name' => $post_name, | |
'post_content' => $post_content, | |
'tags_input' => $tags_input, | |
'post_date' => $post_date, | |
'post_status' => 'draft', | |
'post_author' => 1, | |
'post_category' => array(35) | |
); | |
/* echo '<pre>'; | |
print_r($this_post); | |
echo '</pre>'; | |
exit; */ | |
//insert the post data | |
$result = wp_insert_post($this_post); | |
//grab the inserted post id & add meta data related with post | |
if($result>0) { | |
$inserted_post_id = $result; | |
//add all post meta info | |
add_post_meta( $inserted_post_id, 'content_type', $post_type ); | |
add_post_meta( $inserted_post_id, 'agency', $post_agency ); | |
add_post_meta( $inserted_post_id, 'byline', $post_byline ); | |
add_post_meta( $inserted_post_id, 'video_url', $video_url ); | |
add_post_meta( $inserted_post_id, 'old_content_id', $old_content_id ); | |
//update the list of inserted post ids | |
$inserted_post_ids[] = $inserted_post_id; | |
} | |
} | |
} // end post insertion | |
} | |
//if the records inserted, display list of post ids inserted | |
if(count($inserted_post_ids)>0){ | |
echo "<ol>"; | |
foreach($inserted_post_ids as $post_id){ | |
echo "<li> Video Post inserted with id: $post_id </li>"; | |
} | |
echo "</ol>"; | |
} else { | |
echo '<p> No new Video posts found at IBNLive'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment