I have a custom post type with about 20 different metaboxes. What I would like to do is be able to hit "New post" for this type, enter the post title, fill in an unique identifier on one of the metafields (like a Freebase ID), and then go out and query Freebase, automatically bringing in the data for the other 19 metas.
The going out and getting data works just fine, and when I tested it using different meta keys, it works like a charm. But when I use update_post_meta to set the values to the proper keys (ie the ones actually used by my metaboxes), the metadata gets overwritten by the empty metaboxes in the Edit Post form.
I had been hooking the function that gets the external metadata into save_post. Is there a better method? Better place to hook into? Is there an action somewhere between saving the data from the form and the redirect to the edit page where I could perform the update_post_meta function?
Thanks in advance.
UPDATE: Here's the code - shortened, as I figured showing four of the metas was sufficient.
Again, this code works perfectly IF I save the metadata to keys not being used by the theme (ie if I append the word "Test" to each metakey below, it works fine. That's why I'm assuming the metas are being overwritten by the blank meta boxes in the post edit form.)
<?php
function get_album_data($post_id) {
// after the user fills in the Title and ID and hits "Save Draft"
// this function goes out and gets every piece of information available about this album
global $wpdb;
$slug = 'album';
// check whether anything should be done
$_POST += array("{$slug}_edit_nonce" => '');
if ( $slug != $_POST['post_type'] ) {
return;
}
if ( !current_user_can( 'publish_posts', $post_id ) ) {
return;
}
// set a flag so this only runs once
$initial_data_imported = get_post_meta($post_id, 'initial_data_imported', $single);
if ( !empty( $initial_data_imported ) ) {
return;
}
/* Request passes all checks; update the post's metadata */
if (isset($_REQUEST['Album_ID'])) {
$album_id = $_REQUEST['Album_ID'];
// curl request info goes here
$album_artist = $search_results->album->artist;
$album_producer = $search_results->album->producer;
$album_label = $search_results->album->label;
update_post_meta ($post_id, 'Artist', $album_artist);
update_post_meta ($post_id, 'Producer', $album_producer);
update_post_meta ($post_id, 'Label', $album_label);
update_post_meta( $post_id, 'initial_data_imported', true );
}
}
add_action( 'save_post', 'get_album_data' );
?>
