Hi
I have a form with. these 2 related fields:
[cred_field field='company-to-deliver' force_type='field' select_text='--- not set ---'] (many to many)
[cred_field field='@movie-delivery.parent' select_text='--- not set ---' required='false'] (1 to many)
I need to create a CRED_SAVE_DATA script in order to rename the post title I'm creating on the front end.
I already did one for another form:
add_action('cred_save_data', 'update_post_title_for_character_forms', 10, 2);
function update_post_title_for_character_forms($post_id, $form_data) {
if (!isset($form_data['id'])) {
return;
}
$form_id = (int) $form_data['id'];
// Apply same title update rule for forms 12235 and 12236
if (in_array($form_id, [12235, 12236])) {
$character_name = get_post_meta($post_id, 'wpcf-character-name', true);
if (!empty($character_name)) {
wp_update_post([
'ID' => $post_id,
'post_title' => $character_name,
'post_name' => sanitize_title($character_name),
]);
}
}
}
How can I retrieve the 2 related post-title values in my php script ?
Hi,
Welcome to Toolset support. That will be considered a custom development and it is way outside of our support scope.
To get the related post titles inside your cred_save_data hook you can do as follows:
add_action( 'cred_save_data', function( $post_id, $form_data ) {
// if you want to limit to a specific form, check $form_data['id'] here
// 1) 1-to-many parent (your relationship slug: movie-delivery)
// Returns the parent post ID of the current $post_id.
$movie_id = function_exists('toolset_get_related_post')
? (int) toolset_get_related_post( $post_id, 'movie-delivery' )
: 0;
$movie_title = $movie_id ? get_the_title( $movie_id ) : '';
// 2) many-to-many (your relationship slug: company-to-deliver)
// Get related company IDs; role may be 'parent' or 'child' depending on how the relationship is defined.
$company_ids = function_exists('toolset_get_related_posts')
? (array) toolset_get_related_posts( $post_id, 'company-to-deliver', [
'role' => 'parent', // try 'child' if this returns empty
'return' => 'ids',
'limit' => -1,
])
: [];
// If you expect a single company:
$company_id = $company_ids ? (int) $company_ids[0] : 0;
$company_title = $company_id ? get_the_title( $company_id ) : '';
// If you expect multiple companies, titles as an array:
$company_titles = array_map( 'get_the_title', array_map( 'intval', $company_ids ) );
// Now you have: $movie_title and either $company_title or $company_titles
// …use them to build your post title if you wish.
}, 120, 2 ); // use a later priority so relationships are already saved
Replace the relationship slugs (movie-delivery, company-to-deliver) with yours if different.
If the M2M call with 'role' => 'parent' returns empty, switch it to 'child'.
Priority 120 helps ensure the relationship selections are saved before you read them.
The code above is not tested, It is just an attempt to help you get started to retrieve the post titles.
If you need further help you might consider hiring a developer.
Thanks.
Hi Christopher,
I used
$movie_id = function_exists('toolset_get_related_post')
? (int) toolset_get_related_post($post_id, 'movie-delivery')
: 0;
$movie_title = $movie_id ? get_the_title($movie_id) : '';
But unfortunately it does not retrieve the related movie-title value...
regarding the field "company-to-deliver" it is actually a post-reference field from which I'd like to retrieve the value...
Could you please help me to finalise this "small details" ?
Hi,
I did a quick search and found this:
https://toolset.com/fr/forums/topic/how-to-retrieve-cpt-post-title-of-related-post-to-use-in-another-post/
So basically, the "cred_submit_complete" seems to be a better hook to work from.
add_action( 'cred_submit_complete', function( $form_data, $post_id ) {
// If needed, limit to your form ID:
// if ( (int) $form_data['id'] !== 12345 ) return;
// 1) Parent Movie (1-to-many: @movie-delivery.parent)
$movie_id = function_exists('toolset_get_related_post')
? (int) toolset_get_related_post( $post_id, 'movie-delivery', 'parent' )
: 0;
$movie_title = $movie_id ? get_the_title( $movie_id ) : '';
// 2) Company (Post Reference Field: 'company-to-deliver' => meta key 'wpcf-company-to-deliver')
$company_id = (int) get_post_meta( $post_id, 'wpcf-company-to-deliver', true );
$company_title = $company_id ? get_the_title( $company_id ) : '';
// Example: rename the current post using the two titles
if ( $movie_title || $company_title ) {
$new = trim( $company_title . ( $company_title && $movie_title ? ' — ' : '' ) . $movie_title );
wp_update_post( array(
'ID' => $post_id,
'post_title' => $new,
'post_name' => sanitize_title( $new ),
) );
}
}, 100, 2 );
Thanks.