Wordpress - db_insert_error when using insert_post

wp insert post meta
wp_insert_post
wordpress can t insert image into post
wp_insert_post post status
wordpress post status
wordpress wp_posts post_status
unable to upload wordpress
wordpress custom post status

I'm currently using a CSV file, which is auto-updated daily, to create or update Wordpress posts for a client's website. The CSV has headers for the listing's address, city, state, etc., and each row is another listing. Currently, there are about 220 properties in the CSV. All but three of them are getting created as posts, but 3 of them aren't, and I'm getting a WP_Error like so -->

 WP_Error { 
       "errors"=> array{ ["db_insert_error"]=> 
                array{ [0]=> "Could not insert post into the database" }}
       "error_data"=> array { } 
       "ID"=> int(0) 
       "filter"=> "raw"
 }

I know that the plugin at least works since all the others are getting posted, but it seems that these three for some reason are getting this error. They don't have any special characters in them or other data that would differentiate them from the other rows. Here's my array and code I'm using to create the new post:

$new_post = array(
        'post_title'   => $title,
        'post_content' => wpautop(convert_chars($data['csv_post_post'])),
        'post_status'  => $opt_draft,
        'post_type'    => 'listing',
        'post_date'    => $this->parse_date($data['csv_post_date']),
        'post_excerpt' => '....',
        'post_name'    => $data['csv_post_slug'],
        'post_author'  => $this->get_auth_id($data['csv_post_author']),
        'tax_input'    => $this->get_taxonomies($data),
        'post_parent'  => $data['csv_post_parent'],
);
$pricecheck = trim($data['_listing_price']);

Where $title is a pre-joined string of the address, city, and state.

if (!get_page_by_title( $new_post['post_title'], 'OBJECT', 'listing') && $pricecheck != "") {
    $id = wp_insert_post($new_post, true);
}

I know that none of the above fields or variables are empty, and it gets to wp_insert_post() fine, but it won't insert. Any help would be greatly appreciated!


I got a similar problem today so I'll share what lead me to the solution (the solution might be different for you, but the way will help).

I got the same error message with absolutely no helpful text, so I dug into the WordPress source code and started debugging.

The error is thrown in only one place in wp_insert_post(), when $wpdb->insert() returns false:

if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
    if ( $wp_error ) {
        return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
    } else {
        return 0;
    }
}

$wpdb->insert() makes some sanity checks before doing the insert, using process_fields() in wp-includes/wp-db.php.

protected function process_fields( $table, $data, $format ) {
    $data = $this->process_field_formats( $data, $format );
    if ( false === $data ) {
        return false;
    }

    $data = $this->process_field_charsets( $data, $table );
    if ( false === $data ) {
        return false;
    }

    $data = $this->process_field_lengths( $data, $table );
    if ( false === $data ) {
        return false;
    }

    $converted_data = $this->strip_invalid_text( $data );

    if ( $data !== $converted_data ) {
        return false;
    }

    return $data;
}

I started adding var_dump($data) between each check and the following if-clause to see where the error came from.

In my case the problem was after the strip_invalid_text() call, which lead me to the cause of my problem:

I was reading the data to insert from a different database and it came in the wrong encoding. After adding charset=utf8mb4 to my PDO constructor all data was in utf8 and the wp_insert_post() worked instantly.

It is hard to say what caused the problem in your case, it could be a wrong charset, a wrong format, a field that's too long ... WordPress doesn't really discloses what's the problem. To find it you really have to look closely.

Issue with wp_insert_post and post_content field error Could not , Today, I was trying to insert an imported data into WordPress. I was using wp_insert_post to insert data, and it threw the following error: By default, wp_insert_post will not insert an empty post. This can cause unexpected problems if you’re passing an ID and expecting it to fall back to wp_update_post. For instance, the following will not work: wp_insert_post( array( 'ID' => 2, 'post_parent' => 1 ) ); You will need to call wp_update_post directly.


I had a similar problem and the cause of it was that the post_status parameter was more than 20 characters long.

https://codex.wordpress.org/Post_Status

Hope it helps!

wp_insert_post() | Function, If the current user doesn't have the capability to work with a taxonomy, then you must use wp_set_object_terms() instead. page_template: If post_type is 'page', will  Using PHP Code Snippet. If you’ve tried to insert coding in WordPress, you may notice how it will either strip certain elements or prevent the use altogether. Perhaps one of the best ways to get around this problem is by using PHP Code Snippet. This plugin will take your snippets of PHP code and turn them into usable shortcodes for WordPress.


Make sure the encoding of the data being updated matches. In my case I needed to convert ISO-8859-1 data into UTF-8 multibyte on the array you pass to wp_update_post or wp_insert_post

$post_update_array = array(
'ID' => $post_id,
'post_title' => mb_convert_encoding($post->post_title, 'UTF-8', 'auto'),
'post_content' => mb_convert_encoding($post->post_content, 'UTF-8', 'auto'),
'post_category' => $categories_ids

);

Not working if Posts inserted with 'wp_insert_post , [This thread is closed.] If i create posts with “wp_insert_pos”t and write the Shortcode to “post_content”, expiration is not… Thanks immensely for the article. Although my problem wasn’t identical to the problems given, changing the hosts file managed to get me pointing at the new (unpropogated) version of the web site and replacing localhost with the ip address in wp-config took care of all the problems with pointing to the active version of the site.


wp_insert_post() – Safely insert/update post in the database , WordPress does not do this automatically. This can be easily done by using wp_strip_all_tags(), especially when users of yor site can publish  The wp_insert_post action fires once a post has been saved. You have the ability to set it to only fire on new posts or on all save actions using the parameters. You have the ability to set it to only fire on new posts or on all save actions using the parameters.


wp_insert_post | function | WordPress, If equal to something other than 0, the post with that ID will be updated. return new WP_Error('db_insert_error', __('Could not insert post into the database'),  Using Word has several advantages over using the default WordPress editor. First, you can work offline. It also becomes easier to collaborate on a post or document with others in the draft phase–simply keep the Word draft copy in a sharable or cloud location and share the document with others.


wp_insert_post(), wp_insert_post( array $postarr, bool $wp_error = false ) If equal to something other than 0, the post with that ID will be updated. return new WP_Error( '​db_insert_error' , __( 'Could not insert post into the database' ), $wpdb ->​last_error ); a post_status of 'future' you must specify the post_date in order for WordPress to