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).

2 Answers 2

function transaction_title( $title )

    if ( 'transaction' == get_current_screen()->post_type && '' == $title )
        return 'Transaction '.uniqid();
    // else return the normal title
        return $title;
add_filter( 'title_save_pre', 'transaction_title' );
  • Didn't seem to work, but thanks for the push in the right direction. Got it working now. Just needed to do a simple check on the title.
    – Andrew
    Jul 25, 2012 at 21:55
  • In this case, just edit my answer and make it correct - at least this answers should help later users too :)
    – kaiser
    Jul 25, 2012 at 21:56
  • 1
    Oops good point, I'll update yours now :)
    – Andrew
    Jul 25, 2012 at 21:57

Have you tried using wp_insert_post_data() ? This question may also help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.