动作挂钩save_post在保存时被调用,但是我不知道您现在是否可以添加元数据。但是在用action hook保存帖子后,应该可以创建/更新元数据updated_post_meta。
编辑
要在创建后的屏幕上预先选择一些元字段(自定义字段),您必须首先添加这些元值并添加一个空值。
如果查看文件中的post_custom_meta_box()函数(这是使用的metabox的回调postcustom),则wp-admin/includes/meta-boxes.php可以看到该函数正在list_meta()用于创建预选的元字段。
现在,让我们看一下程序流程,直到显示此元框为止(我们正在寻找可以在此处使用的动作/过滤器挂钩):
- WordPress加载文件
post-new.php
- 该文件
39根据功能在数据库中生成默认帖子get_default_post_to_edit()。真好。基本上,帖子已经作为自动草稿存储在数据库中。不幸的是,目前没有任何挂钩可以更改这些数据或添加新内容。
- 下一步,
edit-form-advaned.php包括文件。该文件将生成Hole admin页面,并包括基于supports帖子类型参数的所有必需的metabox 。
- 在线
136包含自定义字段metabox postcustom,并调用上述函数。同样,没有可以使用的动作挂钩。
结论
我认为唯一的方法是使用jQuery或重载postcustommetabox并在运行list_meta()函数之前添加meta值。
例如
add_action('admin_menu', 'wpse29358_replaceMetaBoxes'); // maybe add_meta_boxes hook
function wpse29358_replaceMetaBoxes() {
remove_meta_box('postcustom', {POST_TYPE}, 'normal');
add_meta_box('postcustom', __('Custom Fields'), 'wpse29358_postcustomMetabox', {POST_TYPE}, 'normal', 'core');
}
function wpse29358_postcustomMetabox($post) {
// Add your meta data to the post with the ID $post->ID
add_post_meta($post->ID, 'key', 'value');
// and then copy&past the metabox content from the function post_custom_meta_box()
}