0

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

I have a table in the database called 'wp_company_profiles' with fields 'address_1' and 'address_2', and have two advanced custom fields set up with the same values. I am trying to put together a hook that will pull data from the database to occupy the custom fields with when a particular post is displayed.

Below is the code I am using, but I'm sure there is something I'm missing but can't figure it out.

Any help would be appreciated. Thanks.

function get_fields_company_profile($post_ID)  {
    global $wpdb;

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

    $row = $wpdb->get_row( "SELECT * FROM wp_company_profiles WHERE pid => '$post_ID' LIMIT 1" );

    $address1 = $row->address_1;
    $address2 = $row->address_2;

    update_post_meta($post_ID, 'address_1', $address1);
    update_post_meta($post_ID, 'address_2', $address2);

}
add_action('init', 'get_fields_company_profile');
  • 1
    Are you sure the query returns a result? Print the $row to see the result. I can see the query being wrong. Check this WHERE pid => '$post_ID'. Should not this be WHERE pid = '$post_ID'? This is assuming you have only one such record for a post and retrieving value for that post only. Also not sure why you are using update_post_meta when you are speaking about displaying the data. – Chittaranjan Dec 11 '13 at 13:41
  • Thanks. I didn't notice the '>' after the equals. Have fixed that but still not working. I'm using update_post_meta as there are custom fields set with values 'address_1' and 'address_2'. I want to update these fields with what is currently in the database. Posts for this particular post type are being entered through a web form, which only retrieves the title and content, whereas the rest of the data (i.e. address 1, address 2) gets put into the database with no way for the client to update the data themselves. – chrisgrace Dec 11 '13 at 14:20
  • You have still not mentioned whether the above query returned any result or not. Print the result in the code, then confirm here. Another question: when there is only one record for a post, why you are using LIMIT 1? Also where this data will be shown: on admin custom post edit screen or on some other place like frontend? – Chittaranjan Dec 11 '13 at 14:36
  • Where is this code intended to run? As is, it runs on every page load, front and back. I am sure that that is wrong and I am sure for some page loads it generating at the least a string of Notices that you would see if you debugging turned on – s_ha_dum Dec 11 '13 at 15:04
  • You've also got a typo in there: your function parameter is called $post_ID, but two lines down you use $post_id. Variable names in PHP are case-sensitive. – Simon Blackbourn Dec 11 '13 at 15:08

Your Answer

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

Browse other questions tagged or ask your own question.