0

I'm having a real problem updating custom fields (within a custom post type) with data from the database.

I have a table in the database called wp_company_profiles with address fields (i.e. address_1, address_2). I have set custom fields for each in the admin post area with the same values. I am trying to put together a hook that will retrieve the data from the database to update the custom fields.

Below is the code, but I can't figure out what its missing.

function get_fields_company_profile($post_id)  {

    global $wpdb;

    if ($parent_id = wp_is_post_revision( $post_id )) $postid = $parent_id;

    $postid = $_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('init', 'get_fields_company_profile');
0

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');
  • Thanks Shazzad. That makes a lot of sense. I knew I couldn't use init in the long term, was only using that for testing as I didn't know which hook to use at this point. The only reason I used $postid is because the query wouldn't accept $post_id, but the init hook is obviously why. Can you please suggest an action hook I should use? save_post or pre_post_update is no good as I need the hook to fire on page load. The custom fields need the data before the post is saved. – chrisgrace Dec 12 '13 at 0:12
  • So do you want to update the postmeta from your custom table when an user is on the post edit page ? ex: "yoursite.com/wp-admin/post.php?post=337&action=edit" ? – Shazzad Dec 12 '13 at 0:16
  • Yes exactly that. – chrisgrace Dec 12 '13 at 1:04
  • Hook to - 'load-post.php' – Shazzad Dec 12 '13 at 2:00
  • Brilliant!! Solved. Thank you Shazzad, thats worked! – chrisgrace Dec 12 '13 at 9:53

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.