嗯,您可以在保存帖子时使用save_post
过滤器-View Codex
Codex Example
/**
* Save post metadata when a post is saved.
*
* @param int $post_id The ID of the post.
*/
function save_book_meta( $post_id ) {
/*
* In production code, $slug should be set only once in the plugin,
* preferably as a class property, rather than in each function that needs it.
*/
$slug = \'book\';
// If this isn\'t a \'book\' post, don\'t update it.
if ( $slug != $_POST[\'post_type\'] ) {
return;
}
// - Update the post\'s metadata.
if ( isset( $_REQUEST[\'book_author\'] ) ) {
update_post_meta( $post_id, \'book_author\', sanitize_text_field( $_REQUEST[\'book_author\'] ) );
}
if ( isset( $_REQUEST[\'publisher\'] ) ) {
update_post_meta( $post_id, \'publisher\', sanitize_text_field( $_REQUEST[\'publisher\'] ) );
}
// Checkboxes are present if checked, absent if not.
if ( isset( $_REQUEST[\'inprint\'] ) ) {
update_post_meta( $post_id, \'inprint\', TRUE );
} else {
update_post_meta( $post_id, \'inprint\', FALSE );
}
}
add_action( \'save_post\', \'save_book_meta\' );
如上所示,您可以
$_REQUEST
发布元数据,根据需要对其进行更改或修改,然后保存。