帖子标题(post_title
) 未保存在元数据中;它是post表中的一个字段。
这是原始代码的更新版本。
add_action( \'save_post\', \'wpse246957_update_post_info\', 10, 3 );
function wpse246957_update_post_info( $post_id, $post, $update ) {
// Stop anything from happening if revision
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// unhook this function so it doesn\'t loop infinitely
remove_action( \'save_post\', \'wpse246957_update_post_info\' );
// get post type
$post_type = get_post_type( $post_id );
// If this isn\'t a custom post, don\'t update it.
// if ( "cbre_access_form" != $post_type ) return;
// run codes based on post status
$post_status = get_post_status();
if ( $post_status != \'draft\' ) {
if ( isset( $_POST[\'post_title\'] ) ) {
$suffix = \' - \' . $post_id;
if ( ! preg_match( \'/\' . preg_quote( $suffix, \'/\' ) . \'$/\', $_POST[\'post_title\'] ) ) {
wp_update_post( [
"ID" => $post_id,
"post_title" => $_POST[\'post_title\'] . $suffix,
] );
}
}
}
// re-hook this function
add_action( \'save_post\', \'wpse246957_update_post_info\', 10, 3 );
}
就个人而言,我可能会在将标题输出到模板文件之前或在输出标题的任何地方添加后缀,因为在重新保存帖子标题时,上述方法可能存在一些边缘情况。