views:

710

answers:

5

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

+5  A: 

You can use the Post object:

// Create post object
  $my_post = array();
  $my_post['post_title'] = 'My post';
  $my_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.

Chuck Conway
And which files I have to include? Because I want to do a separate admin panel for wordpress
Uffo
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
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
+7  A: 

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.

philfreo
I am somewhat new to Wordpress and I am wondering why you recommend against using a basic SQL insert?
ssergei
A: 

Well, what if I am saving all my tips in a MySQL database and would like to post them all at once in Wordpress? It is a simpel insert statement but will it lead to complications?

ThinkCode
Please do not post your question as an answer.
Török Gábor
A: 

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;
Andre D Spencer
A: 

I think it should read as follows: $my_post['post_content'] instead of $my_post['content'].

Todd