Skip Navigation

[Resolved] Populate Title based on 2 fields, one which is a post reference to another CPT

This support ticket is created 3 years, 12 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.29 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by toddU 3 years, 12 months ago.

Assisted by: Waqar.

Author
Posts
#1604421

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

function set_auto_title_flight_fn( $data, $postarr )
{
if( ($data['post_type'] == 'flight') && ($data['post_status'] != 'auto-draft') ) {

// 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"???

#1605279

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

For the post reference field, the selected post's ID is saved as a custom field value.

Once you'll have the ID of the selected airline post, you can easily get value from any of its custom fields, including the "airline-code" field.
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ )

Example:


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

regards,
Waqar

#1605663

Works perfectly. Thank you so much for your help!

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.