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.