wpdb->insert not working

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: []

SHOW FULL COLUMNS FROM `wp_posts`

$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;
$wpdb->show_errors();

$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);
$wpdb->print_error();
var_dump($wpdb);

$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

Leave a Comment