【发布时间】:2010-12-12 20:28:02
【问题描述】:
有人知道如何使用 sql 在 Wordpress 中插入新帖子吗?
【问题讨论】:
-
我编辑了你的问题。如果这不是您所要求的,请恢复它。
有人知道如何使用 sql 在 Wordpress 中插入新帖子吗?
【问题讨论】:
您可以使用 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。
【讨论】:
您的问题询问如何使用 SQL 将新帖子插入 WordPress。如果您真的想这样做,请查看“wp”数据库表并执行标准 INSERT - 这并不难。
但我强烈建议不要这样做 - 即使您想在正常的 WP 提供的管理仪表板之外创建一个单独的管理仪表板,您也应该使用他们提供的 core functions/API提供。例如,wp_insert_post 函数就是您要使用的函数。
我相信您可以通过包含 /wp-load.php 来使用/加载这些功能。
【讨论】:
我首先导出“wp_post”表只是为了查看结构 - 然后复制第一部分并编写第二部分;
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:我从另一个表中获取了我想要插入的内容 - 但您可以在语句内部或外部设置变量,只需将变量设置为您想要的 -
$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;
【讨论】: