2009-11-03 63 views

回答

28

可以使用Post对象:

// Create post object 
    $my_post = array(); 
    $my_post['post_title'] = 'My post'; 
    $my_post['post_content'] = 'This is my post.'; 
    $my_post['post_status'] = 'publish'; 
    $my_post['post_author'] = 1; 
    $my_post['post_category'] = array(8,39); 

// Insert the post into the database 
    wp_insert_post($my_post); 

更多信息发现here

+0

我必须包含哪些文件?因为我想为wordpress – Uffo 2009-11-03 23:49:32

+6

单独管理面板,所以如果你想测试这个,在你的博客的根目录下创建一个“test.php”文件,并在这段代码之前使用这一行:require('./ wp-博客 - header.php文件'); – Volomike 2010-01-27 00:04:44

+2

该类别需要是一个类别ID数组。这并不容易,特别是如果你还没有关联与类别相关的帖子。对类别赋值的修正是这样的:$ sCategory ='Test'; $ asPost ['post_category'] = array($ wpdb-> get_var(“SELECT term_id FROM $ wpdb-> terms WHERE name ='$ sCategory' 0,0)); – Volomike 2010-02-12 02:52:07

13

您的问题问如何使用SQL在WordPress中插入新帖子。如果你真的想这样做,看一下“wp”数据库​​表并执行一个标准的INSERT - 这并不难。

可是我最好强烈建议不要这样做 - 即使你想正常的WP-提供一个之外创建一个单独的管理仪表板,你应该使用core functions/API,他们提供的。例如,wp_insert_post函数就是你想要使用的。

我相信你可以使用/加载这些函数,包括/wp-load.php。

+2

我对WordPress有点新,我想知道为什么你建议不要使用基本的SQL插入? – ssergei 2010-06-18 19:21:52

+1

用于链接到wp_insert_post函数的+1以及用于独立脚本的文件的详细信息 – 2012-11-19 12:56:42

+1

@ssergei,因为Wordpress为您提供了一个API。 – shankshera 2013-10-26 09:14:27

6

我通过导出“wp_post”表开始查看结构 - 然后复制第一部分并写入第二个;

1:用一个变量,你可以使用你的INSERT语句($ SQL)

$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 "; 

2开始:我把我想要的插入,从另一个表的内容 - 但你可以只设置内部或外部的声明变量刚才设置的变量你的愿望 -

$sql .= "(' ','".$post_author."',"."'".$post_date."',"."'".$post_date_gmt."',"."'".$post_content."',"."'".$post_title."',"."'".$post_excerpt."',"."'".$post_status."',"."'".$comment_status."',"."'".$ping_status."',"."'".$posd_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."'),"; 

在此之后,使用标准的查询:

$res = mysql_query($sql); if($res): print 'Successful Insert'; else: print 'Unable to update table'; endif; 
+2

它不起作用,因为没有分类给该文章,因此它将不会正确显示... – 2013-02-05 16:08:41