This has worked for me and i could create now a post title from 4 different fields. The only problem is that the code works only if i create a new post from WordPress dashboard then i can i see the right title. but If i create a new post with CRED form from the front end, it generates a new Post with such title ( CRED Auto Draft d0f8b201bbf07f2b377cf4b7b72ef75d ). then i need to open the post in WordPress Dashboard and click UPDATE to show the right Title and then copy the new Title and change it in the post link too and save again.
Please let me know how to solve this!!
My Code:
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 ); // Grabs the inserted post data so you can modify it.
function modify_post_title( $data, $postarr )
{
//$post_id = $postarr['ID'];
if($data['post_type'] == '0km' && isset($postarr['wpcf']) ) {
$field_1 = $postarr['wpcf']['car-manufacturer'];
$field_2 = $postarr['wpcf']['car-name'];
$field_3 = $postarr['wpcf']['car-version'];
$field_4 = $postarr['wpcf']['model-year'];
$title = '';
if(isset($field_1) && $field_1 != '')
$title .= $field_1 . ' ';
if(isset($field_2) && $field_2 != '')
$title .= $field_2 . ' ';
if(isset($field_3) && $field_3 != '')
$title .= $field_3 . ' ';
if(isset($field_4) && $field_4 != '')
$title .= $field_4 . ' ';
if(isset($field_5) && $field_5 != '')
$title .= $field_5;
$data['post_title'] = $title ; //Updates the post title to your new title.
}
return $data; // Returns the modified data.
}