Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted
function transaction_title( $title )
{


    if ( 'transaction' == get_current_screen()->post_type && '' == $title )
    {
        return 'Transaction '.uniqid();
    }
    // else return the normal title
    else 
    {
        return $title;
    }
}
add_filter( 'title_save_pre', 'transaction_title' );
share|improve this answer
    
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 '12 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 '12 at 21:56
1  
Oops good point, I'll update yours now :) –  Andrew Jul 25 '12 at 21:57
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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