Problem: I have a Form that creates child posts. When the child post is published, I would like to send an automatic email notification to an email address specified in the parent post's custom fields, but I cannot specify that field in the notification settings.
Solution: You can use some custom PHP code with the cred_notification_recipients API and the toolset_get_related_post API to query the parent post, get the custom field value, and add that email address to the recipients list programmatically:
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
// Check notification name matches target notification
if ( isset($notification['name']) && 'your-notification-name' == $notification['name'] ) {
// Add email recipient
$parent_id = toolset_get_related_post(
$post_id,
'your-relationship-slug'
);
$emailemailangehorige = get_post_meta($parent_id, 'wpcf-your-email-field-slug', true);
$recipients[] = array(
'to' => 'to',
'address' => $emailemailangehorige,
'name' => '',
'lastname' => '');
}
return $recipients;
}
Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients