I managed to make it work but not fully.
First, the code you provided is not retrieving an already saved post_meta.
I based my code in the introductory code of this tutorial:
http://wpengineer.com/2076/add-custom-field-attachment-in-wordpress/
And am using the regular post_tag taxonomy instead of a custom one.
And, lastly, there’s a bug, after you close the Media Upload iframe and hit “Update” the tags are erased (no idea why), but if you simply refresh the browser, the tags are there.
Anyway, there are one piece that may contribute to a complete answer:
– intval($post['ancestors'][0]
) in the function wp_set_post_terms
add_filter( 'attachment_fields_to_edit', 'fb_attachment_fields_edit', 10, 2);
add_filter( 'attachment_fields_to_save', 'fb_attachment_fields_save', 10, 2);
function fb_attachment_fields_edit($form_fields, $post) {
$form_fields['artist_credit']['label'] = __( 'Example Custom Field', '' );
$form_fields['artist_credit']['value'] = get_post_meta($post->ID, 'artist_credit', true);
$form_fields['artist_credit']['helps'] = __( 'A helpful text for this field.', '' );
return $form_fields;
}
// save custom field to post_meta
function fb_attachment_fields_save($post, $attachment) {
if ( isset($attachment['artist_credit']) && '' !== $attachment['artist_credit'] ) {
update_post_meta($post['ID'], 'artist_credit', $attachment['artist_credit']);
$check = wp_set_post_terms( intval($post['ancestors'][0]), $attachment['artist_credit'], 'post_tag', true );
}
return $post;
}