wp_insert_post
runs when a post is updated as well. If you scan the source you will see numerous blocks of code switching between an update or not an update. In fact, wp_update_post()
just passes data, after a little processing, to wp_insert_post()
for the bulk of the work. That is why your code runs more than you think it should– the hook doesn’t work as you think it should. Fortunately, WordPress passes a parametere telling you if the hook is being used on an update or not.
do_action( 'wp_insert_post', $post_ID, $post, $update );
So…
function update_profile_record( $post_ID, $post, $update ) {
if (true == $update) return;
//if ($post->post_type="post") return;
$pro_realname = get_post_meta( $post_id, 'realname', true );
$pro_cell = get_post_meta( $post_id, 'cell', true );
$pro_email = get_post_meta( $post_id, 'e_mail', true );
$pro_subdate = get_post_meta( $post_id, 'submit_date', true );
if ($pro_subdate == '') {
$pro_subdate = get_the_date( $format, $post_id );
}
global $wpdb;
$table_name = $wpdb->prefix . "profile_record";
$wpdb->insert(
$table_name,
array(
'rec_profile_name' => $pro_realname,
'rec_profile_contact' => $pro_cell,
'rec_profile_email' => $pro_email,
'rec_profile_date' => $pro_subdate,
)
);
}
add_action( 'wp_insert_post', 'update_profile_record', 10, 3 );