您可以完全避免使用SQL,并使用XML-RPC API。这也可以让您发布到远程wordpress安装。
(注意,如果XML-RPC不是选项,请向下滚动)
If Using XML-RPC
以下是使用XML-RPC快速搜索google以发布到远程Wordpress博客的一些代码:
http://en.forums.wordpress.com/topic/great-code-for-remote-posting?replies=5
下面是一组简单的示例,解释了XML-RPC API
http://life.mysiteonline.org/archives/161-Automatic-Post-Creation-with-Wordpress,-PHP,-and-XML-RPC.html
下面是一个使用Curl和XML-RPC的WP配方示例:
http://www.wprecipes.com/post-on-your-wordpress-blog-using-php
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords=\'\',$encoding=\'UTF-8\') {
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$content = array(
\'title\'=>$title,
\'description\'=>$body,
\'mt_allow_comments\'=>0, // 1 to allow comments
\'mt_allow_pings\'=>0, // 1 to allow trackbacks
\'post_type\'=>\'post\',
\'mt_keywords\'=>$keywords,
\'categories\'=>array($category)
);
$params = array(0,$username,$password,$content,true);
$request = xmlrpc_encode_request(\'metaWeblog.newPost\',$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);
return $results;
?>
If afterall that XML-RPC is not for you
也许wordpress多站点最适合你?创建新帖子就像打电话一样简单
switch_to_blog($blog_id);
然后在打电话之前创建新帖子
restore_current_blog();
If you MUST use SQL
使用
this question linked to 通过mike23以通过wpdb对象访问