Inserting a post in Wordpress using MySql

2019-01-17 01:15发布

Does anyone know how to insert a new post into Wordpress using sql?

3条回答
欢心
2楼-- · 2019-01-17 01:52

I started by exporting the "wp_post" table just to see the structure - then copied the first section and wrote the seccond;

1: Start with a variable you can use for your insert statement ($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 -

$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;
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-17 01:55

You can use the Post object:

// 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.

查看更多
别忘想泡老子
4楼-- · 2019-01-17 02:00

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.

But I'd strongly recommend 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_post function is what you want to use.

I believe you can use/load these functions by including /wp-load.php.

查看更多
登录 后发表回答