Support Forum
Support › MB Relationships › Create/Update CPT title from relationship titleResolved
I have 2 CPT's:
Here's my code to create the relationship:
add_action( 'mb_relationships_init', function () {
MB_Relationships_API::register( [
'id' => 'lead_to_activities',
'from' => [
'object_type' => 'post',
'post_type' => 'lead',
'meta_box' => [
'title' => 'Activities',
],
],
'to' => [
'object_type' => 'post',
'post_type' => 'activity',
'meta_box' => [
'title' => 'Lead',
],
],
] );
} );
When I create a new Activity or update an existing Activity, I want to update the post title/name/slug using values from the custom fields and relationship. I've figured out how to create/update the post title using the custom field values, but just can't figure out how to get the post title from the relationship.
Here's my create/update title code so far:
function iswp_update_activity_title( $data ) {
if( $data['post_type'] == 'activity' ) { // Check post type
if( isset( $_POST['activity_date'] ) ) {
$title = $_POST['activity_date'] . " | " . $_POST['activity_action'] . " | " . $lead;
$slug = sanitize_title_with_dashes( $title,'','save' );
$slugsan = sanitize_title( $slug );
$data['post_title'] = $title ; //Updates the post title to your new title.
$data['post_name'] = $slugsan; //Updates the post name aka slug to sanitized version of title.
}
}
return $data; // Returns the modified data.
}
add_filter( 'wp_insert_post_data' , 'iswp_update_activity_title');
I just can't figure out what I'm supposed to do to get the $lead
to actually return the post title from the related Lead.
Hi,
You can use $_POST['lead_to_activities']
to get the submitted relationship value, it is an array of posts (pages) ID.
Array
(
[0] => 1925
[1] => 1841
)
Loop through the array and use the function get_the_title() to get the post title by ID.
Please can you give an example of the code to achieve this? It is exactly what I need to do but I cannot work it out from your answer above.