3

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;
4

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

0

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 Sep 24 '15 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 Sep 24 '15 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 – Pmpr Dec 17 '15 at 22:21

Your Answer

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

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