我当前在附件的表单中添加了一个自定义字段,名为artist_credit
使用挂钩attachment_fields_to_edit
. 当一个人在艺术家信用字段中输入一个名字时,我希望它(如果它还没有标签)在保存附件时为附件的标签分配一个具有该名字的标签。
我有插件Wordpress Media Tags 已安装,允许我在附件表单上有一个字段,用于将条款附加到media_tag
分类学我发现的问题是当我用钩子的时候attachment_fields_to_save
. 保存的元数据artist_credit
很好(使用update_post_meta
), 只是当我使用wp_set_object_terms
然后添加artist_credit
到media_tag
分类法,它确实分配了它(标记是在我回显时分配的get_the_terms
对于附件帖子),但当我再次转到附件的编辑表单查看结果时,额外的信用标签根本没有分配给附件帖子。
我唯一的理论是media_tag
分类法值在attachment_fields_to_save
这是可以理解的false
值不附加术语。有趣的是,钩子save_post
在编辑附件的详细信息后未被激发,因此我似乎无法使用该挂钩添加此artist_credit
对的值media_tag
此附件帖子的分类。对我能做什么有什么建议吗?
以下是我目前的做法:
// Add custom fields to attachments
function example_add_attachment_fields($form_fields, $post) {
// Create artist_credit custom field
$form_fields[\'artist_credit\'] = array(
\'label\' => \'Artist Credit\',
\'input\' => \'text\',
);
return $form_fields;
}
add_filter(\'attachment_fields_to_edit\', \'example_add_attachment_fields\', null, 2);
// Save attachment\'s custom fields\' values
function example_save_attachment_fields($post, $attachment) {
// Save extra attachment fields
if ( isset($attachment[\'artist_credit\']) ) {
update_post_meta($post[\'ID\'], \'artist_credit\', $attachment[\'artist_credit\']);
// Add artist_credit as a term to the attachment post
wp_set_object_terms( $post[\'ID\'], $attachment[\'artist_credit\'], \'media_tag\', true );
}
return $post;
}
add_filter(\'attachment_fields_to_save\', \'example_save_attachment_fields\', null, 2);