// func that is going to set our title of our customer magically
function w2w_customers_set_title( $data , $postarr ) {
// We only care if it's our customer
if( $data[ 'post_type' ] === 'w2w-customers' ) {
// get the customer name from _POST or from post_meta
$customer_name = ( ! empty( $_POST[ 'customer_name' ] ) ) ? $_POST[ 'customer_name' ] : get_post_meta( $postarr[ 'ID' ], 'customer_name', true );
// if the name is not empty, we want to set the title
if( $customer_name !== '' ) {
// sanitize name for title
$data[ 'post_title' ] = $customer_name;
// sanitize the name for the slug
$data[ 'post_name' ] = sanitize_title( sanitize_title_with_dashes( $customer_name, '', 'save' ) );
}
}
return $data;
}
add_filter( 'wp_insert_post_data' , 'w2w_customers_set_title' , '99', 2 );