您可以使用save_post 钩
save_post
是在创建或更新帖子或页面时触发的操作,可以从导入、帖子/页面编辑表单、xmlrpc或通过电子邮件发布。
add_action( \'save_post\', \'wpse254017_set_post_title\', 10, 2 );
function wpse254017_set_post_title ( $post_id, $post ){
//This temporarily removes action to prevent infinite loops
remove_action( \'save_post\', \'wpse254017_set_post_title\' );
if ( \'my-custom-post-type\' !== $post->post_type )
return;
//GET THE CUSTOM FIELD
$post_title = get_post_meta( $post_id, \'apt_first_name\', true );
//UPDATE TITLE
wp_update_post( array(
\'ID\' => $post_id,
\'post_title\' => $post_title,
));
//redo action
add_action( \'save_post\', \'wpse254017_set_post_title\', 10, 2 );
}
还可以重新写入挂钩,指定post\\U类型
save_post_{post_type}
, 这样,就不会在所有post\\u类型上运行挂钩。
add_action( \'save_post_{post_type}\', \'wpse254017_set_post_title\' );
function wpse254017_set_post_title ( $post_id ){
//This temporarily removes action to prevent infinite loops
remove_action( \'save_post_{post_type}\', \'wpse254017_set_post_title\' );
//GET THE CUSTOM FIELD
$post_title = get_post_meta( $post_id, \'apt_first_name\', true );
//UPDATE TITLE
wp_update_post( array(
\'ID\' => $post_id,
\'post_title\' => $post_title,
));
//redo action
add_action( \'save_post_{post_type}\', \'wpse254017_set_post_title\');
}