<?php
require_once($_SERVER['DOCUMENT_ROOT']."/wp-load.php");
require_once($_SERVER['DOCUMENT_ROOT']."/wp-admin/includes/taxonomy.php");

$title=$_POST['post_title'];
$slug=substr($_POST['post_name'], 0, 200);
$content = $_POST['post_content'];
$date = $_POST['post_date'];
$post_author = $_POST['post_author'];

$catids = explode(',', $_POST['cat_id']);

date_default_timezone_set('Europe/London');
global $wpdb;
$cquery = $wpdb->get_results("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '".$slug."' AND post_type = 'post'");
if ( $cquery ) 
{
	echo $cquery[0]->ID;
}else
{
	// Create post
	$id = wp_insert_post(array(
		'post_title'    => $title,
		'post_content'  => $content,
		'post_name'     => $slug,
		'post_date'     => $date,
		'post_date_gmt' => $date,
		'post_author'   => '1',
		'post_excerpt' => '',
		'post_status' => 'publish',
		'comment_status' => 'closed',
		'ping_status' => 'closed',
		'post_modified' => $date,
		'post_modified_gmt' => $date,
		'post_parent' => 0,
		'post_type' => 'post',				    
		'comment_count' => 0
	));
	
	if ($id) {	
			$category_id = get_cat_ID( 'Press Release' );
			wp_set_post_terms($id, array($category_id), 'category');	
		echo $id;
	} else {
		echo "WARNING: Failed to insert post into WordPress\n";
	}
}
$wpdb->close();
?>