Inserting a post in WordPress using MySql

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

Related posts

Leave a Reply

3 comments

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

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

  3. 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;