I have a custom post type called transaction
, and on the post type's edit/publish screen I have removed the post title field. The post title is now being generated by the function below, which labels each post Transaction
followed by a unique string, thanks to the uniqid
PHP function.
I'm running this function on the title_save_pre
filter but every time I update the post, a new unique number being generated for the title.
How can I modify the code below so that the unique number for the title is only generated once?
function transaction_title( $title ) {
$screen = get_current_screen();
$transaction_number = uniqid(); // unique number
if ( 'transaction' == $screen->post_type ) {
return 'Transaction '. $transaction_number;
} else {
return $title; // else return the normal title
}
}
add_filter( 'title_save_pre', 'transaction_title');
My initial thoughts are to set a post meta flag like this but I'm having trouble combining it with my function. It seems that it only updates the post meta when I use the transition_post_status
action and doesn't with my title_save_pre
filter (which I need to change the title).