如何使用php脚本批量导入wordpress文章

以下是源码,请注意改db和密码,代码转自csdn。

<?php
//fabu(‘你好我是标题’,’你好我是内容’,’你好我是分类’,’你好我是tag1,你好我是tag2′);
function fabu($title,$content,$category,$tag){
$title = my_real_escape_string($title);
$content = my_real_escape_string($content);
$category = $category;
$tag = $tag;
$time = date(‘Y-m-d H:i:s’);
$sql = “INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES (NULL, 1, ‘$time’, ‘$time’, ‘$content’, ‘$title’, ”, ‘publish’, ‘open’, ‘open’, ”, ”, ”, ”, ‘$time’, ‘$time’, ”, ‘0’, ”, ‘0’, ‘post’, ”, ‘0’);”;
$res = handle($sql);
if($res == true){
$res = handle(‘select max(id) from wp_posts;’);
$id = intval( $res[0][0] );
}else{
return false;
}
$termIdArr = array();
$categoryId = getTerm_id($category,0);
$tagArr = explode(‘,’, $tag);
if(!empty($tagArr)){
foreach ($tagArr as $value) {
$termIdArr[] = getTerm_id($value,1);
}
}
}

function handle($sql,$database = ‘database’)
{
$db = mysql_connect(‘127.0.0.1:3306′,’root’, ‘root’);
mysql_select_db($database);
mysql_query(“SET NAMES ‘utf8′”,$db);
if(!$db)
{
echo “Error! Can’t connect RS database!”;
return false;
}
$res = mysql_query($sql ,$db );
if($res === false)
{
echo “Query Failed!$sql\n”;
return false;
}
mysql_close($db);
if($res === true){
return true;
}
$arrRes = array();
while($row = mysql_fetch_row($res))
{
$arrRes[] = $row;
}
return $arrRes;
}

function my_real_escape_string($str)
{
return strtr($str, array(
“\x00” => ‘\x00’,
“\n” => ‘\n’,
“\r” => ‘\r’,
‘\\’ => ‘\\\\’,
“‘” => “\'”,
‘”‘ => ‘\”‘,
“\x1a” => ‘\x1a’
));
}

function getTerm_id($name ,$type){
if($type == 0){
$default = 1;
}else{
$default = 9;
}
$res = handle(“select term_id from wp_terms where name = ‘$name’;”);
if(empty($res)){
$slug = strtolower(urlencode($name));
$sql = “INSERT INTO wp_terms (`term_id`,`name`,`slug`) VALUES (NULL,’$name’,’$slug’);”;
if(true == handle($sql)){
$res = handle(‘select max(term_id) from wp_terms;’);
$id = $res[0][0];
return intval($id);
}
}else{
return intval( $res[0][0] );
}
return $default;
}
?>

 

 

转自csdn