$wpdb insert not working
I've created a new meta box for my 'cases' post type. And when I publish a new 'cases' post I want to insert the meta id into a new table created by me called 'sort'. I used this code:
function save_postdata( $post_id ) {
global $post, $new_meta_boxes, $wpdb;
foreach ($new_meta_boxes as $meta_box) {
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( 'cases' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}
$data = $_POST[$meta_box['name'].'_value'];
if(get_post_meta($post_id, $meta_box['name'].'_value') == "") {
add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
$meta_id = get_post_meta($post_id, 'm开发者_开发知识库eta_id', true);
$wpdb->insert( 'sort', array('meta_id'=>$meta_id, 'column_order' => 1));
}
elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
update_post_meta($post_id, $meta_box['name'].'_value', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
}
}
The sort table has three fields: id, meta_id and column_order. Can someone sees what I'm doing wrong?
Make sure that on your insert statement you add 'NULL'
$wpdb->insert( 'sort', array('NULL', 'meta_id'=>$meta_id, 'column_order' => 1));
This could be causing the issue, and also make sure that it's the correct order of your columns.
精彩评论