23

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

1
  • I edited your question. If it's not what you are asking please revert it. Nov 3, 2009 at 23:40

3 Answers 3

36

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.

5
  • And which files I have to include? Because I want to do a separate admin panel for wordpress
    – Uffo
    Nov 3, 2009 at 23:49
  • 8
    BTW, 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');
    – Volomike
    Jan 27, 2010 at 0:04
  • 2
    The 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));
    – Volomike
    Feb 12, 2010 at 2:52
  • This is great! You saved the day :)
    – Rishabh
    Dec 5, 2017 at 11:58
  • 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
14

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.

4
  • 3
    I am somewhat new to Wordpress and I am wondering why you recommend against using a basic SQL insert?
    – ssergei
    Jun 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
    @ssergei because Wordpress provides you with an API.
    – shankshera
    Oct 26, 2013 at 9:14
  • +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.
    – djjeck
    May 4, 2020 at 21:29
10

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.