Create/Update CPT title from relationship title
Support › MB Relationships › Create/Update CPT title from relationship titleResolved
- This topic has 2 replies, 3 voices, and was last updated 4 years ago by
esmrosc.
-
AuthorPosts
-
September 12, 2020 at 4:33 AM #21835
Nick
ParticipantI have 2 CPT's:
- Lead
- Activity
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
$leadto actually return the post title from the related Lead.September 12, 2020 at 3:37 PM #21838Long Nguyen
ModeratorHi,
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.
February 2, 2022 at 3:51 PM #33617esmrosc
ParticipantPlease 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.
-
AuthorPosts
- You must be logged in to reply to this topic.