I am trying to auto-populate a post title based on 2 custom fields. One of the fields is a Number field and the other is a Post Reference field to another CPT.
CPT is "flight"
Number field is "flight-number"
Post Reference field is "airline" that is connected to CPT "Airlines"
Example of what I'm trying to do:
Create new "flight"; enter "2216" for flight number; select "Southwest Airlines" for airline; click Update
Expect to see for the post title "Southwest Airlines 2216" ....or even better would be "WN 2216"
Note: The "WN" is a custom field in "Airlines" that has been entered for Southwest Airlines. It's the standard 2-letter code for Southwest Airlines in the aviation industry. In CPT "Airlines", this 2 letter code is the field "airline-code".
So far, the only code I can get to work can use only ONE custom field. In this case, the flight-number field.
// Auto Generate Title for Flight
add_filter( 'wp_insert_post_data' , 'set_auto_title_flight_fn' , '99', 2 );
// get the value from the field
$field_value = $postarr['wpcf']['flight-number'];
//Updates the post title to the new value
$data['post_title'] = $field_value ;
}
return $data; // Returns the modified data.
}
// End Generation
Any suggestions on how to go about adding a second field that is a Post Reference field (Airline)? And preferably ... instead of "Airline", use "airline-code" from the CPT "Airlines" that's related through the Post Reference relationship "Airline"???
// Auto Generate Title for Flight
add_filter( 'wp_insert_post_data' , 'set_auto_title_flight_fn' , '99', 2 );
function set_auto_title_flight_fn( $data, $postarr )
{
if( ($data['post_type'] == 'flight') && ($data['post_status'] != 'auto-draft') ) {
// get the value from the "flight-number" field
$flight_number = $postarr['wpcf']['flight-number'];
// get the value from the "airline" field
$airline_post_id = $postarr['wpcf']['airline'];
// get the value from the "airline-code" field
$airline_code = types_render_field("airline-code", array("item" => $airline_post_id));
//Updates the post title to the new value
$data['post_title'] = $airline_code.' '.$flight_number ;
}
return $data; // Returns the modified data.
}
// End Generation
Note: Please make sure to update the field slugs, as used on your website.
I hope this helps and let me know if you need any further assistance around this.