Sep 142011
 

This post will show you how to insert a post into WordPress from a XML feed. I use this to populate my www.caniplay.co.uk website with products from the MediFusion feeds.

 

The posts are not finished when inserted, hence why I make them draft posts. They still need a category assigning to them and any other SEO type stuff I want to add.

 

What I do on www.caniplay.co.uk is attach this code to a hidden page and call it with a start date and end date. This will then add all the products from the feed within that date. I then go through each draft post, tidy it up and publish it to the site.

 

I find this the easiest way to add new posts to my site, as I just go into the drafts folder on WordPress and if there are posts in their I know they need tidying up and adding to the site.

 

UPDATE: I have now added to the code, it now also takes the associated JPG image and uploads that and also links it to the post you are creating. It does a quick check to make sure there is a image (some games have no image associated with them in the feed) and if there is a image URL then it uses it.

 

<?php

$xml = simplexml_load_file("http://www.find-games.co.uk/services/gamesearch.asp?mode=heavy&site=YOURTAGHERE&platform=ps3&category=consoles");

foreach ($xml->children() as $item)
{

	// Create post object
	$my_post = array(
		'post_title' => $item->groupTitle,
		'post_content' => $item->description . '[get_details ASIN="' . $item->id . '"]',
    	'post_status' => 'draft',
    	'post_author' => 1
	);

$new_post = wp_insert_post( $my_post );

if(!empty($item->imageURL))
{
	$file_url = media_sideload_image($item->imageURL,$new_post);
	echo $new_post . ' ' . $item->imageURL . ' ' . $file_url;
}
else
{
	echo $new_post;
}

?>