Firstly, init hook doesn’t supply $post_id
as an argument.
Secondly, you have miss-matched variable name – $postid
& $post_id
Here’s the revised code –
function get_fields_company_profile()
{
if(
isset($_GET['post']) && '' != $_GET['post']
&& isset($_GET['action']) && 'edit' == $_GET['action']
&& ( !isset($_GET['post_type']) || 'post' == $_GET['post_type'] )
){
global $wpdb;
$postid = (int) $_GET['post'];
$row = $wpdb->get_row( "SELECT * FROM wp_company_profiles WHERE pid = '$postid'" );
if ($row)
{
// retrieve fields from database
$address1 = $row->address_1;
$address2 = $row->address_2;
$postcode = $row->postcode;
$city = $row->city;
// update custom fields in admin area
update_post_meta($post_id, 'address_1', $address1);
update_post_meta($post_id, 'address_2', $address2);
update_post_meta($post_id, 'postcode', $postcode);
update_post_meta($post_id, 'city', $city);
}
}
}
add_action('load-post.php', 'get_fields_company_profile');