我有一個WordPress應用程序託管在Azure上,我需要使用自定義函數將數據插入到wp_posts表中,因爲我將此腳本作爲後臺作業運行我不能使用WordPress的功能,因爲腳本不在WordPress的文件夾。如何使用自定義函數將數據插入到wordpress表中
我已經試過:
mysql_query("INSERT INTO wp_posts(post_title, post_content, post_status, post_type, comment_status, page_template), VALUES($title, $sum, 'publish', 'post', 'closed', 'content.php')");
,但我得到了以下錯誤:PHP的警告:請求mysql_query():試圖通過其訪問權限不允許的方式來訪問一個插座。
日誌文件如下:
[04/07/2014 09:51:47 > adf9f5: SYS INFO] Status changed to Initializing
[04/07/2014 09:51:47 > adf9f5: SYS INFO] Run script 'data.php' with script host - 'PhpScriptHost'
[04/07/2014 09:51:47 > adf9f5: SYS INFO] Status changed to Running
[04/07/2014 09:51:48 > adf9f5: ERR ] PHP Warning: mysql_query(): An attempt was made to access a socket in a way forbidden by its access permissions.
[04/07/2014 09:51:48 > adf9f5: ERR ] in C:\DWASFiles\Sites\abacuskenya\Temp\jobs\triggered\y\rdksuocv.xbx\data.php on line 50
[04/07/2014 09:51:48 > adf9f5: ERR ] PHP Warning: mysql_query(): A link to the server could not be established in C:\DWASFiles\Sites\abacuskenya\Temp\jobs\triggered\y\rdksuocv.xbx\data.php on line 50
[04/07/2014 09:51:48 > adf9f5: ERR ] PHP Warning: mysql_insert_id(): An attempt was made to access a socket in a way forbidden by its access permissions.
如何讓我的腳本中使用WordPress的比的wp_insert_post其他的自定義功能
我data.php如下:將數據插入到該表:
<?php
define('DB_NAME', '**********');
define('DB_USER', '********');
define('DB_PASSWORD', '**************');
define('DB_HOST', '*******');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if(!$db_selected){
die('Cannot use '. DB_NAME . ': ' .mysql_error());
}
$pages = array("page0", "page1", "page2", "page3", "page4");
foreach($pages as $page){
$json_feed = "http://digitalrand.net/api/url_data/?key=*****&pass=*****%&".$page;
$json = file_get_contents($json_feed);
$obj = json_decode($json, true);
foreach($obj as $article_array){
$url = $article_array['url'];
$domain = $article_array['domain'];
$favicon = $article_array['favicon'];
$title = $article_array['title'];
$category = $article_array['category'];
$large_summary = $article_array['summary'];
$sum = implode(',',$large_summary);
$images = $article_array['images'];
$image_array = reset($images);
$image = $image_array["image"];
$sql = "INSERT INTO wp_posts(post_title, post_content, post_status, post_type, comment_status, page_template), VALUES($title, $sum, 'publish', 'post', 'closed', 'content.php')";
echo $sql;
mysql_query($sql);
$post_id = mysql_insert_id();
echo "The Post ID is " . $post_id . "<br>";
$data = array($favicon, $domain, $image);
$value = implode(',',$data);
//$a = add_post_meta($post_id, 'post_favicon_domain_image', $value, true);
$sql2 = "INSERT INTO wp_postmeta(post_id, meta_key, meta_value), VALUES($post_id, 'post_favicon_domain_image', $value)";
mysql_query($sql2);
//echo $a;
}
}
您可以通過使用WP API遠程創建內容。手動插入數據到數據庫並不健康。如果手動插入,Action,Hooks,Filters和wp系統上的所有組件都不起作用。如果您使用API,則所有系統都會像在正常的內容創建中一樣工作。查看我的回答,例如 –