您可以通过将一个简单的函数挂接到save_post
钩
add_action(\'save_post\',\'my_rating_field\');
function my_rating_field($post_id){
global $post;
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) )
return;
// Check post type
if ($post->post_type != "YOUR_POST_TYPE_NAME")
return;
// OK, we\'re authenticated: we need to find and save the data
$rating = get_post_meta($post_id,\'rating\',true);
//if field not exists create it and give it the value of one
if (empty($rating) || !isset($rating)){
update_post_meta($post_id,\'rating\',1);
}
}