I have a code that is driving me very crazy and frustrated, I wonder if there will be any help for a newbie like me. There where solutions for something like it before but they are not enough.
I want to be able to auto generate custom post titles with the author first name, last name, a random number and current date. The problem is, it works only when I press publish. But not when I save as draft without publishing finally. I want to be able to generate on draft and when publish it will still maintain the generated title instead of updating each time.
my code is here:
function set_interview_title( $value, $post_id ) {
$label = 'INTERVIEW';
$date = date("Ymd");
$date = date("Ymd", strtotime($date));
$uniqueid_length = 8;
$uniqueid = crypt(uniqid(rand(),1));
$uniqueid = strip_tags(stripslashes($uniqueid));
$uniqueid = str_replace(".","",$uniqueid);
$uniqueid = strrev(str_replace("/","",$uniqueid));
$uniqueid = substr($uniqueid,0,$uniqueid_length);
$uniqueid = strtoupper($uniqueid);
if($value['post_type'] == 'interviews') {
if( ( 'publish' == $_POST['post_status'] ) && ( 'publish' == $_POST['original_post_status'] ) ) {
return;
}
if( ( 'publish' == $_POST['post_status'] ) && ( 'publish' != $_POST['original_post_status'] ) ) {
$title = $label . ' - ' . $uniqueid . '-' . $date;
$post_slug = sanitize_title_with_dashes ($title,'','save');
$post_slugsan = sanitize_title($post_slug);
$value['post_title'] = $title;
$value['post_name'] = $post_slugsan;
}
if( ( 'draft' == $_POST['post_status'] ) || ( 'pending' == $_POST['post_status'] ) ) {
$title = $label . ' - ' . $uniqueid . '-' . $date;
$post_slug = sanitize_title_with_dashes ($title,'','save');
$post_slugsan = sanitize_title($post_slug);
$value['post_title'] = $title;
$value['post_name'] = $post_slugsan;
}
} return $value;
}
add_filter( 'wp_insert_post_data' , 'set_interview_title' , '10', 2 );
Thanks