I am trying to add a post via an external file, but I keep getting an error:

WP_Error Object ( [errors] => Array ( [db_insert_error] => Array ( [0] => Could not insert post into the database ) ) [error_data] => Array ( [db_insert_error] => Column count doesn't match value count at row 1 ) )

Here is my PHP code:


global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
$post_id = wp_insert_post($new_post, true);

So far, I have also checked to make sure my file is UTF-8 (it is). I've tried opening it in several editors, resetting the encoding type, and re-saving, since I've read that this can sometimes be the problem, but no dice.

I've double and triple checked that wp-load.php is in the right place. Any help would be appreciated.

  • 2
    Are you trying to use the default post category with your 'post_category' => array( 0 )? If so, try using array() instead; there should never be a category with the ID 0. (If you're trying to set no categories, I'm afraid that, AFAICT, WordPress posts will always have at least one category set, and if you don't provide one it'll use the default category (generally Uncategorized, unless you've renamed it)). – Pat J May 3 '18 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.