wp_insert_post error: ” Content, title, and excerpt are empty.”

I’m trying to insert WooCommerce products into the WordPress database via PHP. A WooCommerce product is just a post with post-type=product, so I thought that this should work:

$mypost = array('post-title' => $secondexplosion[0], 'post-type' => 'product',
   'post-status' => 'publish', 'post-content' => $secondexplosion[1],
   'post-excerpt' => '');
$postid = wp_insert_post($mypost, true);

$secondexplosion is a String array that contains the post-title and post-content; I checked, and it does not contain null values or anything problematic. Why, then, is wp_insert_post returning the error “Content, title, and excerpt are empty”? Thanks much!

Related posts

Leave a Reply

1 comment

  1. The Codex for wp_insert_post has a lot of useful information:

    $post = array(
      'ID'             => [ <post id> ] //Are you updating an existing post?
      'menu_order'     => [ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs.
      'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments.
      'ping_status'    => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off
      'pinged'         => [ ? ] //?
      'post_author'    => [ <user ID> ] //The user ID number of the author.
      'post_category'  => [ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories
      'post_content'   => [ <the text of the post> ] //The full text of the post.
      'post_date'      => [ Y-m-d H:i:s ] //The time post was made.
      'post_date_gmt'  => [ Y-m-d H:i:s ] //The time post was made, in GMT.
      'post_excerpt'   => [ <an excerpt> ] //For all your post excerpt needs.
      'post_name'      => [ <the name> ] // The name (slug) for your post
      'post_parent'    => [ <post ID> ] //Sets the parent of the new post.
      'post_password'  => [ ? ] //password for post?
      'post_status'    => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | 'custom_registered_status' ] //Set the status of the new post.
      'post_title'     => [ <the title> ] //The title of your post.
      'post_type'      => [ 'post' | 'page' | 'link' | 'nav_menu_item' | 'custom_post_type' ] //You may want to insert a regular post, page, link, a menu item or some custom post type
      'tags_input'     => [ '<tag>, <tag>, <...>' ] //For tags.
      'to_ping'        => [ ? ] //?
      'tax_input'      => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies. 
    );  
    

    It looks like you’ve just got a typo in your fields; the expected field names are post_title post_type post_status post_content and post-excerpt – that’s with underscores, instead of dashes.