Does anyone know how to insert a new post into Wordpress using sql?
3 Answers
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.
-
And which files I have to include? Because I want to do a separate admin panel for wordpress– UffoNov 3, 2009 at 23:49
-
8BTW, if you want to test this, create a "test.php" file in the root of your blog and use this line before this code: require('./wp-blog-header.php');– VolomikeJan 27, 2010 at 0:04
-
2The category needs to be an array of category IDs. That's not easy to get, especially if you don't have posts associated yet with categories. The fix on the category assignment was this: $sCategory='Test';$asPost['post_category']=array($wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE name='$sCategory'",0,0));– VolomikeFeb 12, 2010 at 2:52
-
-
Hi, I'm trying to get this to work inside a plugin but it looks like the plugin code isn't able to use WordPress function. Does anybody have an idea on this? Including wp-load isn't really recommended if i'm right. Sep 20, 2018 at 7:30
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.
-
3I am somewhat new to Wordpress and I am wondering why you recommend against using a basic SQL insert?– ssergeiJun 18, 2010 at 19:21
-
1+1 for link to the wp_insert_post function and the details of the file to include for standalone scripts Nov 19, 2012 at 12:56
-
3
-
+1 to that advice. Simply inserting a new row in wp_posts, and adding to a category in wp_term_relationships, doesn't work: the post count updates, but those posts don't show. I'm not sure what else you need to do nowadays.– djjeckMay 4, 2020 at 21:29
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;
-
2It won't work, because no category has been assigned to the post, and thus it won't be shown correctly...– OrabîgFeb 5, 2013 at 16:08
-
1
-
Some good help in this article as well: linkstraffic.net/transfer-wordpress-posts-one-database Feb 24, 2019 at 10:28