กระดานถามตอบ ( Webboard Q&A )

<?php
// Load WordPress
require_once 'wp-load.php';
require_once ABSPATH . 'wp-admin/includes/taxonomy.php';

date_default_timezone_set('Asia/Bangkok');

$begintime1 = strtotime("12:01:01");
$endtime1 = strtotime("12:59:00");

$begintime2 = strtotime("17:01:01");
$endtime2 = strtotime("24:00:00");

$begintime3 = strtotime("00:00:01");
$endtime3 = strtotime("08:59:59");

$now = time();

$cnt_new_row = 0;

//echo "/".$now."/".$begintime1."/".$endtime1."/".$begintime2."/".$now."/".$endtime2."/<br>";

if (($now >= $begintime1 && $now <= $endtime1) || ($now >= $begintime2 && $now <= $endtime2) || ($now >= $begintime3 && $now <= $endtime3)) {
//echo "Yes, this time for e-GP";

global $wpdb;
$aType = array('P0', '15', 'B0', 'D0', 'W0', 'D1', 'W1', 'D2', 'W2');
$aCat = array(118, 119, 120, 121, 122, 123, 124, 125, 126); //รหัสหมวดหมู่ใน WordPress สำหรับแยกประเภทประกาศ anounceType
$i = 0;

foreach ($aType as $newsType) { //ประเภทประกาศ
//$url = "http://www.taong.go.th/newsfeed.xml"; //ไฟล์ xml สำหรับใช้ทดสอบ
//เทศบาลเมืองเขลางค์นคร=4520101
$depid = '6320106';
$url = 'http://process3.gprocurement.go.th/EPROCRssFeedWeb/egpannouncerss.xml?deptId=' . $depid . '&anounceType=' . $newsType;

libxml_use_internal_errors(true);

$doc = simplexml_load_string(file_get_contents($url));

if ($doc === false) {
libxml_clear_errors();
libxml_use_internal_errors($doc);
} else {

$items = $doc->xpath('/rss/channel/item');
foreach ($items as $item) {
$itemtitle = wp_strip_all_tags($item->title);
$wpdb->show_errors(true);
$sql = $wpdb->prepare("SELECT $wpdb->posts.ID FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id WHERE $wpdb->term_relationships.term_taxonomy_id=" . $aCat[$i] . " AND post_status='publish' AND post_title LIKE '%s'", $itemtitle);
//echo $sql;

$result = $wpdb->get_results($sql);

if (!$result) {
$itemcontent = "<a href=\"" . TRIM($item->link) . "\" targer=\"_blank\">".$itemtitle." คลิกอ่านที่นี่</a>";

$id = wp_insert_post(array(
'post_title' => $itemtitle,
'post_content' => $itemcontent,
'post_date' => date('Y-m-d H:i:s'),
'post_author' => 1,
'post_type' => 'post',
'post_status' => 'publish',
));
if ($id) {
$cnt_new_row += 1;
$cat = array($aCat[$i]);
$setcat = wp_set_post_categories($id, $cat);
} else {
echo "WARNING: Failed to insert post into WordPress\n";
}
}
}
$i++;
}
}

} else {
//echo "Not time to e-GP";
}

?>