EDIT: Rewrote the page from scratch and have been unable to figure out the differences. The new page works as expected.

Checked multiple questions here and also on Wordpress.org with no luck yet. Below is code I'm using in several page to take old records and plug them into the DB (with no issues on those pages). There's more code further in the page but this is where I'm having issues. It's not working on one page.

I'm not getting any errors, even with $wpdb->show_errors(); Only output I'm getting is after I use $wpdb->print_error(), which gives me the output:

`WordPress database error: []


$new_id = $wpdb->insert_id; $new_id at this point is 0 because nothing was inserted into the DB (verified via PHPMyAdmin).

I've tried to indicate what types of data I'm passing in the insert (via third array of '%s', etc) but got the same results.

Any recommendations of other tests or workarounds would be appreciated.


function transfer_officer($old_record) {

global $wpdb;

$status = 'publish';
$old_id = $old_record[0];
$name = $old_record[1];
$post_date = date('Y-m-d h:i:s');

$post_data = array(
    'post_author' => 1,
    'post_date' => $post_date,
    'post_date_gmt' => $post_date,
    'post_content' => '',
    'post_title' => $name,
    'post_excerpt' => '',
    'post_status' => $status,
    'comment_status' => 'closed',
    'ping_status' => 'closed',
    'post_password' => '',
    'post_name' => officer_name_to_slug($name),
    'to_ping' => '',
    'pinged' => '',
    'post_modified' => $post_date,
    'post_modified_gmt' => $post_date,
    'post_content_filtered' => '',
    'post_parent' => 0,
    'menu_order' => 0,
    'post_type' => 'rb_board_officers_posts',
    'post_mime_type' => '',
    'comment_count' => 0

//put into post table
$wpdb->insert('wp_posts', $post_data);

$new_id = $wpdb->insert_id;

3 Answers 3


I got the same problem.

Solution 1: Strip your data.. Wordpress rejects the query if the length of value is greater than the field length defined on the database.

On your insert query, the value for post_type field exceeded the 20 character limit.

Solution 2: Use the $wpdb->query method


Hopefully, you define global variable = $wpdb.

If your $wpdb not working, and also not showing any error then you must try these three steps.

  1. Print your error using with errors functions.

    echo $wpdb->last_error;


    echo $wpdb->show_errors();

  2. If no error visible then you must print your last query using with last query function.

    echo $wpdb->last_query();

  3. Copy and paste your last query in your Phpmyadmin > Your Database -> Table -> SQL tab, click on Go button, you will definitely get proper solutions(errors) in it.


Rocky Mehta.


Have you tried using manually entered data to see if that gets inserted? Just make some stuff up to replace the variables. Also check that the post_author has permission to insert posts.

Another option is to try wp_insert_post() and see if that works.

  • Replaced the variables with strings, same results. Tried the same with wp_insert_post(), same results.
    – momn99
    Commented Sep 24, 2015 at 14:51
  • 1
    Dave, thanks for the time and help. Restarted the page from scratch and have no issues. No clue as to what I had done differently, but obviously I had.
    – momn99
    Commented Sep 24, 2015 at 15:03
  • Accepting answers has special meaning here in SO. dont use it to thank people for their effort. As I see, this is not the correct answer to you question Commented Dec 17, 2015 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.