Support Forum
Support › MB Relationships › Append a Related Posts title to the title of created Post.Resolved
I would like to use the title of the related post as part of the title of the post being updated / saved / created. The are two CPT's, 'clients' and 'equipment'. Each 'client' can have multiple 'equipment' related to them, but each 'equipment' can only have one 'client'. I would like to have the title of the 'client' appended onto the end of the title of the 'equipment'.
I am using this code to create the title of the 'equipment' from a few of the custom fields and it works well. However I would really like to add in the 'clients' name as well so that it is very clear which 'equipment' belongs to who.
The relationship is called 'clients-and-equipment'
<?php
add_action( 'rwmb_equipment-details_after_save_post', 'update_post_title');
function update_post_title( $post_id ) {
// Get the vl number
$equipmentvl = rwmb_meta( 'equipment_vl_number', '', $post_id );
// Get the serial number
$equipmentserial = rwmb_meta( 'equipment_serial_number', '', $post_id );
// Get the client name - this is the bit I cannot work out how to do
//$equipmentclient =
$post_slug = sanitize_title_with_dashes ($my_post,'','save');
$post_slugsan = sanitize_title($post_slug);
// Prepare update post
$my_post = array(
'ID' => $post_id,
'post_title' => $equipmentvl . ' ' . $equipmentserial . ' ' . $equipmentclient,
'post_name' => $post_slugsan,
);
wp_update_post( $my_post );
}
Any suggestions would be appreciated.
Hi,
Please refer to this topic https://support.metabox.io/topic/create-update-cpt-title-from-relationship-title/
to update the post title with the relationship post title.
Thanks, I have read that topic before so I am sorry for starting a new one, but would you be able to expand upon the code with an example of how to achieve it?
Hi,
Each relationship meta box has an ID: {$relationship_id}_relationships_from
and {$relationship_id}_relationships_to
. You can use this meta box ID with the action rwmb_{$meta_box_id}_after_save_post
. For example:
add_action( 'rwmb_clients-and-equipment_relationships_from_after_save_post', 'update_post_title');
function update_post_title( $post_id ) {
// Get the relation object ID via the key {$relationship_id}_from or {$relationship_id}_to
$related_title_1 = get_the_title( $_POST['clients-and-equipment_from'][0] );
// Get the current post title
$current_post_title = $_POST['post_title']
// Prepare update post
$my_post = array(
'ID' => $post_id,
'post_title' => $current_post_title . ' ' . $related_title_1,
);
wp_update_post( $my_post );
}
Thank you so much, I have been away from my computer for a few weeks but this was perfect. I had to remove the [0] from your code as it seemed to load in 'Hello World' but now it appears to work. Thank you again. The edited code line is below.
// Get the relation object ID via the key {$relationship_id}_from or {$relationship_id}_to
$related_title_1 = get_the_title( $_POST['clients-and-equipment_from'] );
I have recently come back to this again and I am using the following code to use the values from two custom fields and the title of the related post to populate the title of the new post. This works in conjunction with automatically linking the the related post on submission of the form via another snippet. So that when the form is first submitted, the relation is set due the post that the form was submitted on and the values and the relationship create the title.
So the good news is that when the form is first submitted, everything works as expected, and when in the wordpress admin post edit screen everything works as expected, with any changes made to the post reflected in the title.
The problem comes if you use the frontend form to try and update/edit the details of the post on the front end, the edited custom field values are added at the front of the existing title. Then if the post is updated via the admin panel, everything works as expected again, and the title goes back to what it should be. I am not sure quite what causes this but hopefully someone might spot my obvious mistake.
Hopefully the code is of use to somebody else as it stands, it is only not working if using the frontend form to edit the post.
<?php
add_action( 'rwmb_equipment-details_after_save_post', 'update_equipment_post_title');
function update_equipment_post_title( $post_id )
{// Get the vl number
$equipmentvl = rwmb_meta( 'equipment_vl_number', '', $post_id );
// Get the serial number
$equipmentserial = rwmb_meta( 'equipment_serial_number', '', $post_id );
// Get the relation object ID via the key {$relationship_id}_from or {$relationship_id}_to
$related_title_1 = get_the_title( $_POST['clients-and-equipment_from'] );
$post_slug = sanitize_title_with_dashes ($my_post,'','save');
$post_slugsan = sanitize_title($post_slug);
// Prepare update post
$my_post = array(
'ID' => $post_id,
'post_title' => $equipmentvl . ' ' . $equipmentserial . '-' . $related_title_1,
'post_name' => $post_slugsan,
);
wp_update_post ( $my_post ) ;}