我想save_post
动作钩是合适的。如果帖子标题已经设置好,可能需要插入一些检查($post_object->post_title
), 因为这段代码总是根据内容更新标题。
add_action( \'save_post\', \'save_post_wpse_87921\', 10, 2 );
function save_post_wpse_87921( $post_id, $post_object )
{
// Auto save?
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// Correct post_type
if ( \'servicestatus\' != $post_object->post_type )
return;
$new_title = wp_trim_words( $post_object->post_content, $num_words = 10, $more = \'\' );
// Unhook this function so it doesn\'t loop infinitely
remove_action( \'save_post\', \'save_post_wpse_87921\' );
// Call wp_update_post update, which calls save_post again.
wp_update_post( array(
\'ID\' => $post_id,
\'post_title\' => $new_title
));
add_action( \'save_post\', \'save_post_wpse_87921\', 10, 2 );
}