使用 MySql 在 Wordpress 中插入帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1670838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting a post in Wordpress using MySql
提问by Uffo
Does anyone know how to insert a new post into Wordpress using sql?
有谁知道如何使用 sql 在 Wordpress 中插入新帖子?
回答by Chuck Conway
You can use the Post object:
您可以使用 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 );
More info found here.
在这里找到更多信息。
回答by philfreo
Your question asks how to insert a new post into WordPress using SQL. If you really wanted to do that, taking a look at the "wp" database tables and do a standard INSERT - this wouldn't be hard.
您的问题询问如何使用 SQL 将新帖子插入 WordPress。如果您真的想这样做,请查看“wp”数据库表并执行标准 INSERT - 这并不难。
But I'd stronglyrecommend against doing this - even if you want to create a separate admin dashboard outside of the normal WP-provided one, you should use the core functions/API that they provide. For example, the wp_insert_postfunction is what you want to use.
但我强烈建议不要这样做 - 即使您想在 WP 提供的普通仪表板之外创建一个单独的管理仪表板,您也应该使用它们提供的核心功能/API。例如,wp_insert_post函数就是你想要使用的。
I believe you can use/load these functions by including /wp-load.php.
我相信您可以通过包含 /wp-load.php 来使用/加载这些功能。
回答by Andre D Spencer
I started by exporting the "wp_post" table just to see the structure - then copied the first section and wrote the seccond;
我首先导出“wp_post”表只是为了查看结构 - 然后复制第一部分并编写第二部分;
1: Start with a variable you can use for your insert statement ($sql)
1:从一个可以用于插入语句的变量开始($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: I took the content that i wanted to insert, from another table - but you can just set the variables either inside or outside of your statement just set the variable to what you desire -
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."'),";
After that, use your standard query:
之后,使用您的标准查询:
$res = mysql_query($sql); if($res): print 'Successful Insert'; else: print 'Unable to update table'; endif;